MongoDB query to return only embedded document

后端 未结 6 1671
别跟我提以往
别跟我提以往 2020-12-05 15:22

assume that i have a BlogPost model with zero-to-many embedded Comment documents. can i query for and have MongoDB return only Comme

6条回答
  •  余生分开走
    2020-12-05 15:44

    The mongodb javascript shell is documented at http://docs.mongodb.org/manual/reference/method/

    If you want to get back only specific fields of an object, you can use

    db.collection.find( { }, {fieldName:true});
    

    If, on the other hand, you are looking for objects which contain a specific field, you can sue

    db.collection.find( { fieldName : { $exists : true } } );
    

提交回复
热议问题