2017-03-21 8 views
0
{ 
    "_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

利用$elemMatchのすべてのデータ:

db.test.find({}, {community: { $elemMatch: {communityId: "community001"}}}) 
あなただけのアイテムを必要としているものによってドキュメントをフィルタしたい場合は、あなたのフィルターを維持することができます

コミュニティ配列。

+0

HI Sergey - あなたが強調する必要がある重要なことは、あなたが[投影](https://docs.mongodb.com/manual/tutorial/project-fields -from-query-results /); OPはまだ予測に慣れていないようです。 –

関連する問題