我正尝试在mongoose中使用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是否正确地格式化了嵌套的fields对象。
这样做是正确的方式吗?