通过检查子文档中数组(具有子文档)的值来查找用户

时间:2018-11-29 09:46:25

标签: node.js mongodb mongoose

[  
   {  
      "_id":"5ba8e3c7433cd30be05538b7",
      "createdAt":"2018-09-24T07:29:16.188Z",
      "subscription":[  
         {  
            "types":"Basic",
            "registeredOn":1542969566953,
            "expires":1545561566953
         }         {  
            "types":"Utility",
            "registeredOn":1542985801375,
            "expires":1545577801375
         },
         {  
            "types":"Demo",
            "registeredOn":1542985801375,
            "expires":1545577801375
         }
      ]
   }
]   

如何从“订阅”中的许多类型访问具有“ Demo”订阅类型的用户。

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

User.find({"subscription.types": "Demo"},function(err,users){
    if(!err && users){
      return res.json(users);
    }
});