gpt4 book ai didi

node.js - 如何使用node和mongodb进行查找?

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:23 25 4
gpt4 key购买 nike

以下数据存储在我的收藏中

[
{ "_id" : ObjectId("53d9feff55d6b4dd1171dd9e"),"name":"John", "rank":1, "year":1998 ,"class":"a" ,"total":"1128" },
{ "_id" : ObjectId("feff553d95d6b4dd19e171dd"),"name":"Sherif", "rank":1, "year":1999 ,"class":"b" ,"total":"1163"},
{ "_id" : ObjectId("9fef53dd6b4dd1171f55dd9e"),"name":"shravan", "rank":1, "year":2000 ,"class":"b" ,"total":"1113"},
{ "_id" : ObjectId("117153d9fef6b4dddd9ef55d"),"name":"aneesh", "rank":1, "year":2001 ,"class":"d" ,"total":"1145"},
{ "_id" : ObjectId("dd9e53feff55d6b4dd1171d9"),"name":"abdul", "rank":1, "year":1997 ,"class":"a" ,"total":"1100"},
]

我为 find 和 [{ "name":"John", "rank":1, "year":1998 },{ "name":"Sherif", "rank":1, "year":1999 }] 这是我从 req.body 获取的数据,我在这里提到作为查询

router.post('/users',function(req,res){
var query =[{ "name":"John", "rank":1, "year":1998 },{ "name":"Sherif", "rank":1, "year":1999 }]
User.find(query, function(err, result) {
if (err) throw err;
console.log(result);
res.json(result)

});

我期望这个 res.json 应该只返回这两个文档

[
{ "_id" : ObjectId("53d9feff55d6b4dd1171dd9e"),"name":"John", "rank":1, "year":1998 ,"class":"a" ,"total":"1128" },
{ "_id" : ObjectId("feff553d95d6b4dd19e171dd"),"name":"Sherif", "rank":1, "year":1999 ,"class":"b" ,"total":"1163"}]

在mongodb中我们可以这样写

db.users.find({rank:1, name:{$in:["John","Sherif"]}, year:{$in:[1998,1999]}})

我想要nodejs、express中的解决方案,因为根据请求我们需要查找帮帮我吧

最佳答案

尝试 Mongoose $or运算符

var query = {
$or : [
{
"name":"John",
"rank":1,
"year":1998
},
{
"name":"Sherif",
"rank":1, "year":1999
}
]
}

关于node.js - 如何使用node和mongodb进行查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49685529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com