如何使用 node 和 mongodb 进行查找? [英] How to do find using node and mongodb?

查看:51
本文介绍了如何使用 node 和 mongodb 进行查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<预><代码>[{ "_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 }]用户查找(查询,函数(错误,结果){如果(错误)抛出错误;控制台日志(结果);res.json(结果)});

我期望这个 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 查询 = {$或:[{"name":"约翰",等级":1,年":1998},{"name":"谢里夫",等级":1,年份":1999}]}

Below Data stored in my collection

[
{ "_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"},
]

I wrote api for find and [{ "name":"John", "rank":1, "year":1998 },{ "name":"Sherif", "rank":1, "year":1999 }] this is the data i am getting from req.body here i mentioned as a query

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)

  });

My expectation this res.json should return only these two documents

[
{ "_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"}]

In mongodb we can write like this

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

i want solution in nodejs,express because based on request we need to do find Help me out

解决方案

try with mongoose $or operator

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

这篇关于如何使用 node 和 mongodb 进行查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆