1. 程式人生 > >mongodb的聚合函式的$project方法運用。

mongodb的聚合函式的$project方法運用。

譯自:http://docs.mongoing.com/manual-zh/reference/operator/aggregation/project.html

例項如下:

db.books.insert(
{
  "_id" : 1,
  title: "abc123",
  isbn: "0001122223334",
  author: { last: "zzz", first: "aaa" },
  copies: 5
}
)

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )的查詢結果如下:

{ "_id" : 1,
"title" : "abc123", "author" : { "last" : "zzz", "first" : "aaa" } }
如下需要不顯示_id 域(欄位),則需如下指定:

db.books.aggregate( [ { $project : { _id: 0, title : 1 , author : 1 } } ] )

查詢結果如下:


    "title" : "abc123", 
    "author" : {
        "last" : "zzz", 
        "first" : "aaa"
    }
}