I'm using nodejs and the native mongodb driver v2.0.43 (latest as of 9/18/2015) and am having problems querying the db.fs.files collection. It is returning that fs is undefined yet I can use the mongo console and see the files that have been stored using db.fs.files.find().toArray(). Does anyone have any experience querying the db.fs.files collection via node using the mongodb driver?
thanks
Ben
Ok, after doing a better google query I found this solution: Can you use find queries on GridFS using javascript API?
The gist is you have to use 'fs.files' as the collection name.
db.collection('fs.files').find().toArray();
Related
I did my homework before posting this question
So the case is that I want to create a utility in my nodejs application that will move specific collections from my main database to an archive database and vice versa. I am using mongo db atlas for my application. I have been doing my research and I found two possible ways one is to create a mongodump and store and other is to create a backup file myself using my node application and upload it to archive db. Using the later approach will cause to loose my collection indexes.
I am planning to use mongodump for the purpose but can't find a resource that shows how to achieve that. Any help would be appreciated. Also if any one has any experience with similar situation I am open to suggestions as well.
I recently created a mongodump & mongorestore wrapper for nodejs: node-mongotools
What does it mean?
you have to install mongo binary on your host by following official mongo documentation(example) and then, you could use node-mongotools to call them from nodeJS.
Here is an example but tool doc contains more details:
var mt = new MongoTools();
const dumpResult = await mt.mongodump({ uri, path })
.catch(console.log);
I am coming from a Java Hibernate frameworks, where an sql script can be written and added to a project, with the intentions of creating objects in my Mongo Database when the app runs and no data was found in a specific database table. I am using Mongoose presently with mongoDB, and i want to initialize my mongoDB table with objects if none is found. Initially i achieved this my creating an object and calling the new mongoose.save(obj) inside my app.js which is my start up file. But is there a cleaner way one already supported by Mongoose, or is this my best bet ?
I'm attempting to upgrade from Mongoose 4.x to 5.x. So far the biggest issue I'm running in to is after the database has connected I'm unable to fetch collections like I was able to in 4.x.
const someCollection = mongoose.connection.db.collection('someCollection');
This code works fine in 4.x, but apparently the underlying structure of Mongoose has changed between versions.
Is there an equivalent way to do this in Mongoose 5.x? I looked in the documentation and the migration guide but didn't see anything.
For Mongoose 5.x, the .db part isn't needed so it should be:
mongoose.connection.collection('someCollection');
Why is MongoDB usually used in conjunction with NodeJS?, Is it just coincidental or are there good engineering reasons behind this combination?
There is no direct correlation between nodejs and mongo.
In particular mongo has drivers for the following languages:
C
C++
C#
Java
Node.js
Perl
PHP
Python
Ruby
Scala
The only correlation that I can find is that using nodejs queries are more similar to the same query written in the console of mongo than other languages (below an example in nodejs and in java).
In node js:
...
db.collection('restaurants').insertOne( {
"name":"Pizza Roma",
"city":"Rome",
"country":"Italy"
});
...
In java
...
Document restaurant = new Document("name", "Pizza Roma")
.append("city", "Rome")
.append("country", "Italy");
db.getCollection("restaurants").insertOne(document);
...
The structure of MongoDB documents is JSON-like.
JSON (JavaScript Object Notation) is syntactically identical to the code for creating JavaScript objects, so creating JSON structures from objects and parsing JSON to objects is really easy in JavaScript. You can directy insert a JavaScript object structure into MongoDB.
Other than that, MongoDB has Drivers for a vast array of languages.
There is no direct relation between Node.js and Mongodb. MongoDB is a
document based database which will give data as a JSON directly and it is schema less.To develop the rapid applications now a days MEAN stack apps are very famous.Actually Node.js is not limited to MongoDB it can be connected with different databases.
db.gpsvalues.aggregate([
{$sort:{"Timestamp":1}},
{$group:{
"_id":"$EmpID",
LastUpdated:{$last:"$Timestamp"},
Latitude:{$last:"$Latitude"},
Longitude:{$last:"$Longitude"}
}}
])
this is the query which returns expected value in mongodb. but i want to get the same values from node.js api call?
can someone tell me how to make the node.js api?
thanks in advance
You may check the mongojs node.js client which supports aggregation as mentionned in the manual.
I personnally have not used this feature yet but it should work as expected.