【问题标题】:Search for multiple criteria in mongodb在mongodb中搜索多个条件
【发布时间】:2018-11-18 12:48:49
【问题描述】:

我想从用户集合中的状态、金额、创建日期、_id、货币以及名字字段等字段中查找与input search string 匹配的任何文档,我还需要从匹配文档中填充senderIdreceiverId .

这是我的示例文档

{
    "_id" : ObjectId("5a6059701766d72d8872bc75"),
    "bankId" : ObjectId("5a1ff844cb8e4b304e414d51"),
    "senderId" : ObjectId("5a16ca832bedaa6c4cb4ad97"),
    "receiverId" : ObjectId("5a1f9dc3891ee80f0bb4632e"),
    "docurl" : "",
    "isSeen" : true,
    "paidOn" : ISODate("2018-01-18T08:23:12.621Z"),
    "customFees" : [],
    "existingInvoiceUrl" : "",
    "existingInvoice" : "",
    "dueDate" : "2018-01-18",
    "subTotal" : 1000,
    "amount" : 1000,
    "bank" : "ICICI",
    "notes" : "undefined",
    "status" : "pending",
    "currency" : "EURO",
    "updated_date" : ISODate("2018-01-18T08:23:12.621Z"),
    "created_date" : ISODate("2018-01-18T00:00:00.000Z"),
    "__v" : 0,
    "isAdminRead" : true
}

我正在寻找一种有效的方法来使用单个 mongodb 查询来做到这一点。请帮忙:)

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query


    【解决方案1】:

    您可以尝试以下聚合

    您可以使用$match$or 在任何条件中简单地搜索您的字符串...然后您需要执行$lookup 以将您的字段填充到所需的集合中...如果您希望发送者和接收者作为对象然后在管道的末尾使用$unwind 阶段...这里对于created_date,您也可以使用$lte $gte...

    首先你需要检查字符串是否有效objectId

    var mongodb = require("mongodb"),
    objectid = mongodb.BSONPure.ObjectID;
    const myMatch = []
    
    const myArray = [
      { "status": { "$regex": "your_string" }},
      { "amount": { "$regex": "your_string" }},
      { "created_date": : "your_string" },
      { "currency": { "$regex": "your_string" }},
      { "firstName": { "$regex": "your_string" }}
    ]
    
    if (objectid.isValid("your_string")) {
        myArray.push({ "_id": : mongoose.Types.ObjectId("your_string") })
    }
    

    如果你有 mongodb 3.6 版

    db.collection.aggregate([
      { "$match": { "$or": myArray } },
      { "$lookup": {
        "from": Sender.collection.name,
        "let": { "senderId": "$senderId" },
        "pipeline": [
           { "$match": { "$expr": { "$eq": [ "$_id", "$$senderId" ] } } }
         ],
         "as": "sender"
      }},
      { "$lookup": {
        "from": Receiver.collection.name,
        "let": { "receiverId": "$receiverId" },
        "pipeline": [
           { "$match": { "$expr": { "$eq": [ "$_id", "$$receiverId" ] } } }
         ],
         "as": "receiver"
      }}
     ])
    

    如果您拥有 3.6

    之前的 mongodb 版本
    db.collection.aggregate([
      { "$match": { "$or": myArray } },
      { "$lookup": {
        "from": Sender.collection.name,
        "localField": "senderId",
        "foreignField": "_id",
        "as": "sender"
      }},
      { "$lookup": {
        "from": Receiver.collection.name,
        "localField": "receiverId",
        "foreignField": "_id",
        "as": "receiver"
      }}
     ])
    

    【讨论】:

    • 我收到此错误:未捕获的错误:传入的参数必须是 12 个字节的单个字符串或在新 ObjectID 传递的 24 个十六进制字符的字符串 search-string = "pending"
    猜你喜欢
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2020-12-19
    • 1970-01-01
    相关资源
    最近更新 更多