【问题标题】:how filter inside array in mongodb如何在mongodb中过滤数组内部
【发布时间】:2013-05-15 04:18:22
【问题描述】:

我只想从我的查询和预期结果中过滤 kumar "to" items"_id" : ObjectId("5048d2e5fbdac48208000042") 消息

    {
    "_id" : ObjectId("5191502a2f1b3ca33e000016"),
    "message" : "<p>sssdasd<br></p>",
     "subject" : "test message to nag",
"date" : ISODate("2013-05-13T20:42:19.349Z")
    }

来自下面的集合

{
        "__v" : 0,
        "_id" : ObjectId("5191502a2f1b3ca33e000016"),
        "conversation" : [
                {
                        "date" : ISODate("2013-05-13T20:42:19.349Z"),
                        "message" : "<p>sssdasd<br></p>",
                        "_id" : ObjectId("5191502a2f1b3ca33e000017"),
                        "to" : {
                                "_id" : ObjectId("5048d2e5fbdac48208000042"),
                                "name" : "Kumar"
                        },
                        "from" : {
                                "_id" : ObjectId("503fdbfa294f6db74de649ea"),
                                "name" : "Anand"
                        }
                },
                {
                        "message" : "<p>reply</p>",
                        "date" : ISODate("2013-05-13T21:05:33.453Z"),
                        "_id" : ObjectId("5191559c7d2c386741000018"),
                        "to" : {
                                "_id" : ObjectId("503fdbfa294f6db74de649ea"),
                                "name" : "Anand"
                        },
                        "from" : {
                                "_id" : ObjectId("5048d2e5fbdac48208000042"),
                                "name" : "Kumar"
                        }
                },
                {
                        "message" : "<p>reply2<br></p>",
                        "date" : ISODate("2013-05-13T21:05:55.006Z"),
                        "_id" : ObjectId("519155b1ca98b66641000014"),
                        "to" : {
                                "_id" : ObjectId("503fdbfa294f6db74de649ea"),
                                "name" : "Anand"
                        },
                        "from" : {
                                "_id" : ObjectId("503fdbfa294f6db74de649ea"),
                                "name" : "Anand"
                        }
                }
        ],
        "from" : {
                "_id" : ObjectId("503fdbfa294f6db74de649ea"),
                "name" : "Anand"
        },
        "sent" : ISODate("2013-05-13T20:42:19.349Z"),
        "subject" : "test message to nag",
        "to" : {
                "_id" : ObjectId("5048d2e5fbdac48208000042"),
                "name" : "Kumar"
        }
}

我尝试了以下查询

 db.messages.find({'conversation.to._id':ObjectId("5048d2e5fbdac48208000042")},{'subject':-1, 'conversation.message': 1,'conversation.to':1}).pretty();

但我没有得到预期的结果

【问题讨论】:

  • 你需要使用 $elemMatch 投影算子。
  • 能否请您详细给我,因为我在这里尝试相同 db.messages.find({"conversation": { $elemMatch : {"to._id" : ObjectId("5048d2e5fbdac48208000042")} }})。漂亮的();我没有得到结果
  • db.message.find({'conversation.to._id':ObjectId("5048d2e5fbdac48208000042")},{'subject':1, 'conversation.message': 1, 对话: { $ elemMatch : {'to.name' : "Kumar"}}, 'conversation.to':1}).pretty();
  • 它看起来不错,但我想获取对话中的最后一项

标签: mongodb mongoose


【解决方案1】:

我使用了聚合框架,我能够得到预期的结果。

 db.message.aggregate(
                    { $unwind : "$conversation" },
                    { $match: { 
                                    "conversation.to.name" : "Anand"
                                    }
                    },
                    {
                      $sort: {
                                    "conversation.date" : -1 
                      }
                    },
                    {
                      $group: {
                                    _id:"$_id",
                                    msgSubject: {$first:"$subject"},
                                    msgDate: {$first:"$conversation.date"},
                                    latestmsg: {$first:"$conversation.message"}
                      }
                    }
    );

如果与上述不同,请分享

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 2021-05-21
    • 2015-07-24
    • 2020-12-20
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多