Question

I'm new to mongodb and I'm having what seems to be a simple issue but google is giving me a bunch of mapreduce scenarios that doesn't seem relevant.

I have a collection with a document inside. If I try to search by one of the fields, I get an error:

> db.users.find('u_email', "")
error: { "$err" : "ReferenceError: u_email is not defined", "code" : 16722 }

Then mongoDB outputs an error like this:

Tue Dec 03 02:55:13.085 [conn1] assertion 16722 ReferenceError: u_email is not defined ns:test.users query:{ $where: "u_email" }

My mongoose user schema has the u_email field:

var userSchema = new mongoose.Schema({
    'u_id'      : {type:String, unique:true},
    'u_email'   : {type:String, required:true, unique:true}, 
    'u_name'    : {'first'  : {type:String}, //first name
                   'last'   : {type:String} //last name 
                  },
});

Not sure what's going on here. I try to do a findOne() in my app.js with mongoose and it doesn't bring up anything. It's weird because it returns the correct _id but nothing else:

var email = email@dns.com;
User.findOne('u_email', email).exec(function (err, doc) {
    console.log(doc);
}

Result:

{ _id: 529d8cfb03820d5410000002, u_name: {} }

Here's my db.users.getIndexes() for reference:

> db.users.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "test.users",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "u_id" : 1
                },
                "unique" : true,
                "ns" : "test.users",
                "name" : "u_id_1",
                "background" : true,
                "safe" : null
        },
        {
                "v" : 1,
                "key" : {
                        "u_email" : 1
                },
                "unique" : true,
                "ns" : "test.users",
                "name" : "u_email_1",
                "background" : true,
                "safe" : null
        }
]
Was it helpful?

Solution

The query format for the MongoDB shell is { KEY : VALUE, ... }:

> db.users.find({ 'u_email' : '' })

Similarly for Mongoose:

User.findOne({ 'u_email' : email }).exec(function (err, doc) {
  console.log(doc);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top