如何获取第一个数据的集合“社区”

时间:2017-03-21 10:52:01

标签: mongodb

{
    "_id": ObjectId('58d0789eb9031ac824000001'),
    "propertyId": "property001",
    "title": "test1",
    "imageUrl": "",
    "contact": "tom",
    "contactphone": "045182321127",
    "community": [
        {
            "_id": "123456789",
            "communityId": "community001",
            "title": "communitytest",
            "imageUrl": "",
            "contact": "tom",
            "contactphone": "045182321127"          
        },
        {
            "communityId": "community002",
            "title": "communitytest",
            "imageUrl": "",
            "contact": "jack",
            "contactphone": "045182321127"  
                },
                {           
        },
        {
            "communityId": "community003",
            "title": "communitytest3",
            "imageUrl": "",
            "contact": "sfva",
            "contactphone": "045182321127"      
        }
    ]
}

如何获取第一个数据的集合“community”。我使用"db.test.find({"community.communityId":"community001"})"返回"community"

的所有数据

1 个答案:

答案 0 :(得分:0)

如果要限制数组内容,请使用$elemMatch

db.test.find({}, {community: { $elemMatch: {communityId: "community001"}}})

如果您希望按社区数组中只有必需项目的文档过滤文档,则可以保留过滤器。