【问题标题】:Retrive sub-document from collection with condition in mongodb在 mongodb 中使用条件从集合中检索子文档
【发布时间】:2018-07-09 13:44:43
【问题描述】:

belwo 是我的收藏

"_id" : ObjectId("5b435afff64f913c51799334"),
    "_class" : "com.games24x7.mongo.ChatMessageClanId",
    "chatMessage" : [
        {
            "playerId" : NumberLong(230000034),
            "message" : "Hi ",
            "messageType" : "NORMAL",
            "chatTime" : NumberLong("1530894311114"),
            "chatSequence" : 1
        },
        {
            "playerId" : NumberLong(230000034),
            "message" : "Hi ",
            "messageType" : "NORMAL",
            "chatTime" : NumberLong("1530894311114"),
            "chatSequence" : 2
        },
        {
            "playerId" : NumberLong(230000034),
            "message" : "Hi ",
            "messageType" : "NORMAL",
            "chatTime" : NumberLong("1530894311114"),
            "chatSequence" : 3
        },
        {
            "playerId" : NumberLong(230000034),
            "message" : "Hi ",
            "messageType" : "NORMAL",
            "chatTime" : NumberLong("1530894311114"),
            "chatSequence" : 4
        }],
    "clanId" : 1

我想检索以下格式的数据,例如:

"chatMessage" : [{
                "playerId" : NumberLong(230000034),
                "message" : "Hi ",
                "messageType" : "NORMAL",
                "chatTime" : NumberLong("1530894311114"),
                "chatSequence" : 3
            },
            {
                "playerId" : NumberLong(230000034),
                "message" : "Hi ",
                "messageType" : "NORMAL",
                "chatTime" : NumberLong("1530894311114"),
                "chatSequence" : 4
            }]

我尝试过以下查询:

db.chatMessageClanId.find({clanId:1,"chatMessage.chatSequence":{$gt:2}}) db.chatMessageClanId.find({clanId:2,"chatMessage.chatSequence":{$gt:2}},{chatMessage:1})

【问题讨论】:

    标签: arrays mongodb mongodb-query mongotemplate


    【解决方案1】:

    你可以尝试使用$filter聚合

    db.collection.aggregate([
      { $match: { clanId: 1 }},
      {
        $project: {
          chatMessage: {
            $filter: {
              input: "$chatMessage",
              as: "chatMsg",
              cond: {
                $gt: [
                  "$$chatMsg.chatSequence",
                  2
                ]
              }
            }
          },
          _class: 1,
          clanId: 1
        }
      }
    ])
    

    输出

    [
      {
        "_class": "com.games24x7.mongo.ChatMessageClanId",
        "_id": ObjectId("5b435afff64f913c51799334"),
        "chatMessage": [
          {
            "chatSequence": 3,
            "chatTime": 1530894311114,
            "message": "Hi ",
            "messageType": "NORMAL",
            "playerId": 230000034
          },
          {
            "chatSequence": 4,
            "chatTime": 1530894311114,
            "message": "Hi ",
            "messageType": "NORMAL",
            "playerId": 230000034
          }
        ],
        "clanId": 1
      }
    ]
    

    试试here

    【讨论】:

    • 这几乎是正确的,但是有多个文档,所以它提供了所有文档的数据,我只需要从一个文档中获取它。唯一的文档键将是 clanId
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2016-12-29
    相关资源
    最近更新 更多