How do I perform a query in Mongoose?

前端 未结 2 1705
清酒与你
清酒与你 2020-12-30 09:17
> db.users.findOne();
{
    \"_id\" : ObjectId(\"4db8ebb4c693ec0363000001\"),
    \"fb\" : {
        \"name\" : {
            \"last\" : \"Sss\",
            \"fi         


        
相关标签:
2条回答
  • 2020-12-30 09:22

    Try this :

    User.findOne( { $where : "this.fb.gender == 'male' " } )
    

    or

    User.findOne( { fb.gender : "male" } )
    
    0 讨论(0)
  • 2020-12-30 09:45

    I'm one of the authors of mongoose. You can do this query in one of several ways:

    • find syntax

      User.findOne({'fb.gender': 'male'}, callback);
      
    • where syntax

      User.where('fb.gender', 'male').findOne(callback);
      
    • named scope syntax

      UserSchema.namedscope('male').where('fb.gender', 'male');
      // ...
      var User = mongoose.model('User', UserSchema);
      
      // Now you can write queries even more succinctly and idiomatically
      User.male.findOne(callback);
      
    0 讨论(0)
提交回复
热议问题