Node.js Mongodb匹配对象数组
我的模式:Node.js Mongodb匹配对象数组,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我的模式: const UserSchema = new Schema({ firstname: { type: String }, lastname: { type: String } }); 假设我有一个集合users,其中包含以下文档: { "_id" : 1, "firstname" : "hello", "lastname" : "world" } { "_id" : 2, "firstname" : "hello", "lastname" : "earth" } 客户端将
const UserSchema = new Schema({
firstname: { type: String },
lastname: { type: String }
});
假设我有一个集合users
,其中包含以下文档:
{ "_id" : 1, "firstname" : "hello", "lastname" : "world" }
{ "_id" : 2, "firstname" : "hello", "lastname" : "earth" }
客户端将下面的搜索查询发送到我的服务器。
对于mongoose,我需要查询mongodb,它应该返回用户,用户id为1,与第二个对象匹配
[{
firstname: 'hello',
lastname: 'you'
},
{
firstname: 'hello',
lastname: 'world'
}
{
firstname: 'hello',
lastname: 'hello'
}]
请注意,搜索查询不是静态的,它可以是:
[{
firstname: 'fsefsefsef',
lastname: 'fsefsecsfe'
},
{
firstname: 'fsefsef',
lastname: 'esfsf'
}
{
firstname: 'zefzef',
lastname: 'fzefzfz'
}]
在这种情况下,结果应该是一个空数组,因为没有用户匹配查询中的任何对象您可以在
find
方法中传递该对象
User.find(query[1])
我不明白。这要么是一个非常棘手的问题,要么是一个明显的问题:
UserSchema.findOne({firstname:'hello',lastname:'word'},function(err,user){})
@Mikey我需要查询整个数组,我只是尝试使用foreach和promise all,它似乎很有效,但我觉得它有些过分了查询不是我可以控制的,它是从客户端发送的,就像搜索查询,这意味着我无法手动从该查询中获取第二个对象并进行查找;)如果定义了user1
,那么为什么需要查询数组呢。