0

I am running through a tutorial I found online for Node.js and Mongo DB which provides the following code:

    collection.find({},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });

For searching a mongo DB database for all users. The code works fine, however when I tried to modify it to

    collection.find({'username': "testuser1"},{},function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
    });

In order to get one specific user, I am still getting all results.

If I enter that same collection.find() command in to the mongo console, it returns a single result as expected.

What's going on?

2 Answers 2

1

username needs to be in quotes.

collection.find({'username':'testuser1'},{},function(e,docs){
    res.render('userlist', {
        "userlist" : docs
    });
});

... should work.

I believe the reason is because MongoDB uses BSON, which requires the quotes, while JSON doesn't, but I may be mistaken.

Sign up to request clarification or add additional context in comments.

5 Comments

No, that can't be it. Those are equivalent in JavaScript.
@JohnnyHK - but BSON is not JavaScript. MongoDB syntax requires quotes around both key name and definition. The code works with the quotes. Without them, it does not. I wrote the tutorial referenced in the original post, so I was able to quickly bring up the code, edit it, and check for myself.
Also, the original post has been edited. Previously there were no quotes around username, but now there are.
I wonder what's going on, as that just doesn't make any sense. This is using the node.js driver, and as the first parameter is passed to collection.find, it's a JavaScript object, not BSON. And {'x': 'y'} is equivalent to {x: 'y'} in JavaScript so I don't see how this could make a difference. And indeed, when I tested it using the mongodb-native driver they both worked.
You're right. I just checked and it works with or without the quotes. I know that in JS objects the quotes are irrelevant, but I was assuming it was related to BSON, which prefers the quotes. Apparently I'm wrong. So ... I have no idea why my solution worked for the original poster.
0

I used the same tutorial as Chris S.

With this code on my index.js

   /* GET Userlist page. */
   router.get('/userlist1', function(req, res) {
    var db = req.db;
    var collection = db.get('questions1');
    collection.find({},{},function(e,docs){
        res.render('userlist1', {
            "userlist" : docs
        });
    });
    });

I get all the values from my collection. That`s nice, but how to get a single document from that same collection? Or even better, how to get that single document randomly? So that if you query it multiple times the returned value changes every time.

Btw, that tutorial was pretty much perfect for creating something from scratch and somewhat understanding how application ja database connections works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.