mongooseselect字段(嵌套)

我正在尝试在mongoose中使用select运算符来为以下对象select某些字段:

{ "_id" : ObjectId("5249bb97a5de48bda3000003"), "id": 1, "geometry" : { "coordinates" : [ 1, 1 ], "type" : "Point" }, "properties" : { "TYPE" : "Some Type", "TIMESTAMP": ...... }, "type" : "Feature" } 

我想mongo只返回'properties.TYPE'和properties.TIMESTAMP字段。 我可以在mongo中用下面的查询来做到这一点:

 db.features.find({id: 1}, {'properties.TYPE': 1, 'properties.TIMESTAMP': 1}) 

我试图在mongoose中使用select语句来做同样的事情:var fields = {properties:{OBJECTID:1,TIMESTAMP:1}} var query = Feature.find({id:1})。select(fields) ;

Mongo在尝试这样做时会抛出一个错误,所以我不确定mongoose是否正确地格式化嵌套的字段对象。

这是做到这一点的正确方法吗?

你可以在Mongoose的select对象中使用相同的点符号样式,就像你在find例子中一样:

 var fields = { 'properties.OBJECTID': 1, 'properties.TIMESTAMP': 1 }; var query = Feature.find({id: 1}).select(fields); 

您也可以使用Mongoose样式selectstring:

 var query = Feature.find({id: 1}) .select('properties.OBJECTID properties.TIMESTAMP');