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"
    }
}

你可能感兴趣的:(MongoDB)