我正在使用mongoldb数据库,我实际上是这个数据库的新手,我有一组联系人,我想通过mongo查询获取个人数据。
联系人收藏
[{
"_id": "57cd2f6c3966037787ce9550",
"contact": [{
"id": "457899979",
"fullname": "Abcd Hello",
"phonenumber": "123575784565",
"currentUserid": "123456789"
}, {
"id": "7994949849",
"fullname": "Keyboard Mouse",
"phonenumber": "23658974262",
"currentUserid": "123456789"
}, {
"id": "7848848885",
"fullname": "Test Xyz",
"phonenumber": "87556699632",
"currentUserid": "123456789"
}]
}, {
"_id": "57cd2fe02c40b97791b39fe3",
"contact": [{
"id": "457899979",
"fullname": "iPad",
"phonenumber": "85632889714",
"currentUserid": "789456123"
}, {
"id": "7994949849",
"fullname": "Cool",
"phonenumber": "33698777523",
"currentUserid": "789456123"
}]
}]
Mongo查询
db.friendslist.find({"currentUserid" : "789456123"})
但此查询获取空结果, 我想获取currentUserid的联系人列表:789456123。
请仔细阅读我的帖子并向我推荐一些解决方案。
找到解决方案
db.friendslist.find({ "contact": { "$elemMatch": { "currentUserid" : "789456123" } } }).pretty()