Mongodb查询选择嵌入文档

时间:2016-06-01 10:43:41

标签: mongodb mongodb-query

我有以下Mongodb文件。想要获取参与者=' xxx'和(message.touserid =' xxx'或者message.fromuserid =' xxx')。

我正在使用以下查询,但它返回所有消息而不是一个。你能告诉我如何实现这个结果吗?

{ "$and" : [ { "participants" : { "$regex" : "56d314a8e4b04d7f98cfd0c6"} , "$or" : [ { "messages.touserId" : "56d314a8e4b04d7f98cfd0c6"} , { "messages.formuserId" : "56d314a8e4b04d7f98cfd0c6"}]}]} fields: { "_id" : "0" , "product" : "0" , "participants" : "0" , "messages" : "0"}

{
  "_id": {
    "$oid": "574eb878027520c2158268d6"
  },
  "_class": "com.idearealty.product.shopchat.persistence.model.Discussion",
  "participants": "56d314a8e4b04d7f98cfd0c6,56d5d48ee4b0cc330f512a47,56d9d599e4b0cc330f512aaa,57130299e4b08c554c1092c7,56841002eceefce22f455c7f",
  "messages": [
    {
      "_id": {
        "$oid": "574eb874027520c2158268d2"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "56d314a8e4b04d7f98cfd0c6",
      "touser": "debopam_r",
      "message": "Creating Discussion",
      "isMute": false,
      "index": 1,
      "createDate": {
        "$date": "2016-06-01T10:27:00.500Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-01T10:27:00.501Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    },
    {
      "_id": {
        "$oid": "574eb875027520c2158268d3"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "56d5d48ee4b0cc330f512a47",
      "touser": "Raushan",
      "message": "Creating Discussion",
      "isMute": false,
      "index": 2,
      "createDate": {
        "$date": "2016-06-01T10:27:01.295Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-01T10:27:01.295Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    },
    {
      "_id": {
        "$oid": "574eb875027520c2158268d4"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "56d9d599e4b0cc330f512aaa",
      "touser": "anirbanshop1",
      "message": "Creating Discussion",
      "isMute": false,
      "index": 3,
      "createDate": {
        "$date": "2016-06-01T10:27:01.962Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-01T10:27:01.962Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    },
    {
      "_id": {
        "$oid": "574eb876027520c2158268d5"
      },
      "formuserId": "56841002eceefce22f455c7f",
      "fromuser": "9674642375",
      "touserId": "57130299e4b08c554c1092c7",
      "touser": "dummyshop",
      "message": "Creating Discussion",
      "isMute": false,
      "index": 4,
      "createDate": {
        "$date": "2016-06-01T10:27:02.574Z"
      },
      "lastModifiedDate": {
        "$date": "2016-06-01T10:27:02.574Z"
      },
      "createdBy": "9674642375",
      "lastModifiedBy": "9674642375"
    }
  ],
  "messageCount": 4,
  "createDate": {
    "$date": "2016-06-01T10:27:04.041Z"
  },
  "lastModifiedDate": {
    "$date": "2016-06-01T10:27:04.041Z"
  },
  "createdBy": "9674642375",
  "lastModifiedBy": "9674642375"
}

1 个答案:

答案 0 :(得分:1)

由于这是元素的复杂匹配 - 在这种情况下不能使用$elemMatch, 所以聚合框架是一个帮手。

    var match = {
    $match : {
        participants : /56d314a8e4b04d7f98cfd0c6/
    }
}
var unwind = {
    $unwind : "$messages"
}
var matchSecond = {
    $match : {
        $or : [{
                "messages.touserId" : "56d314a8e4b04d7f98cfd0c6"
            }, {
                "messages.formuserId" : "56d314a8e4b04d7f98cfd0c6"
            }
        ]
    }
}
var projection = {
    $project : {
        _id : 0,
        messages : 1
    }
}
db.deb.aggregate([match, unwind, matchSecond, projection])
  

并输出:

{
    "messages" : {
        "_id" : {
            "oid" : "574eb874027520c2158268d2"
        },
        "formuserId" : "56841002eceefce22f455c7f",
        "fromuser" : "9674642375",
        "touserId" : "56d314a8e4b04d7f98cfd0c6",
        "touser" : "debopam_r",
        "message" : "Creating Discussion",
        "isMute" : false,
        "index" : 1.0,
        "createDate" : {
            "date" : "2016-06-01T10:27:00.500Z"
        },
        "lastModifiedDate" : {
            "date" : "2016-06-01T10:27:00.501Z"
        },
        "createdBy" : "9674642375",
        "lastModifiedBy" : "9674642375"
    }
}
相关问题