node.js: Mongodb db.collection.find() not working while collection.insert works
find
returns a cursor, not the matching documents themselves. But a better fit for your case would be to use findOne
:
collection.findOne({name:req.body.name}, function(err, doc) {
if (doc) {
// A doc with the same name already exists
}
});
Tokeloshe
Updated on June 23, 2022Comments
-
Tokeloshe almost 2 years
I'm using node.js/express and I have a Mongodb to store some sets of data. On a webpage the user can enter, edit and delete data (which all works fine). For example, to add data I have the following code:
router.post('/addset', function(req,res) { var db = req.db; var collection = db.get('paramlist'); collection.insert(req.body, function(err, result){ res.send( (err === null) ? { msg: '' } : { msg: err } ); }); });
In my app.js file I include the lines
// Database var mongo = require('mongodb'); var monk = require('monk'); var db = monk('localhost:27017/paramSet1');
as well as
app.use(function(req,res,next){ req.db = db; next(); });
to make the database accessible in the rest of the code (following this tutorial: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ , I'm a beginner with these things).
So all this works fine. My problem is the following: I would like to add a test if a dataset with the same name is already in the database and give a message to the user. Following this answer How to query MongoDB to test if an item exists? I tried using collection.find.limit(1).size() but I get the error
undefined is not a function
I tried the following things. In the cost above (router.post) I tried adding after the line var collection...
var findValue = collection.find({name:req.body.name});
If i then do console.log(findValue), I get a huge output JSON. I then tried console.log(findValue.next()) but I get the same error (undefined is not a function). I also tried
collection.find({name:req.body.name}).limit(1)
as well as
collection.find({name:req.body.name}).limit(1).size()
but also get this error. So in summary, collection.insert, collection.update and collection.remove all work, but find() does not. On the other hand, when I enter the mongo shell, the command works fine.
I would be grateful for any hints and ideas.
Edit: The output to console.log(findValue) is:
{ col: { manager: { driver: [Object], helper: [Object], collections: [Object], options: [Object], _events: {} }, driver: { _construct_args: [], _native: [Object], _emitter: [Object], _state: 2, _connect_args: [Object] }, helper: { toObjectID: [Function], isObjectID: [Function], id: [Object] }, name: 'paramlist', col: { _construct_args: [], _native: [Object], _emitter: [Object], _state: 2, _skin_db: [Object], _collection_args: [Object], id: [Object], emitter: [Object] }, options: {} }, type: 'find', opts: { fields: {}, safe: true }, domain: null, _events: { error: [Function], success: [Function] }, _maxListeners: undefined, emitted: {}, ended: false, success: [Function], error: [Function], complete: [Function], resolve: [Function], fulfill: [Function], reject: [Function], query: { name: 'TestSet1' } }
-
Tokeloshe almost 9 yearsThanks for your answer. That is in fact what I had tried and what gave me the error, but I mistyped it when I posed the question. Very sorry about that. I have edited the question now.
-
tom almost 9 yearsYou say you get huge JSON output. Does it contain the expected data? If it's unreadable, try a JSON formatter like this one.
-
Tokeloshe almost 9 yearsI have added the output in the question. I don't think it contains the expected data.
-
tom almost 9 yearsFrom that output, I don't see a "limit" function returned, but I do see success, error, complete, etc. Perhaps the version of Mongo you have installed is different from the Mongo version your application is using? Try calling one of the methods returned from the find()
-
Tokeloshe almost 9 yearsThank you! That seems to solve my problem. Still not sure I understand why limit(1) doesn't work, but thanks a lot.
-
Prasanth Jaya over 6 yearsBut how do i find multiple records with same name?