猫鼬选择字段(嵌套)

编程入门 行业动态 更新时间:2024-10-19 19:39:57
本文介绍了猫鼬选择字段(嵌套)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图在猫鼬中使用select运算符为以下对象选择某些字段:

I am trying to use the select operator in mongoose to select certain fields for the following object:

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

我想mongo仅返回'properties.TYPE'和properties.TIMESTAMP字段.我可以在mongo中执行以下查询:

I would like to mongo to return only the 'properties.TYPE' and properties.TIMESTAMP fields. I can do this in mongo with the following query:

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

我试图在猫鼬中使用select语句来做同样的事情: var字段= { 属性:{OBJECTID:1,TIMESTAMP:1} } var query = Feature.find({id:1}).select(fields);

I am trying to use the select statement in mongoose to do the same thing: var fields = { properties: { OBJECTID: 1, TIMESTAMP: 1 } } var query = Feature.find({id: 1}).select(fields);

Mongo尝试执行此操作时会引发错误,因此我不确定猫鼬是否正确设置了嵌套字段对象的格式.

Mongo throws an error when trying to do that so I am not sure mongoose is formatting a nested fields object correctly.

这是执行此操作的正确方法吗?

Is this the proper way to do this?

推荐答案

您可以在select对象中的Mongoose中使用与find示例相同的点符号样式:

You can use the same dot notation style in the select object with Mongoose as you do in your find example:

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

您还可以使用猫鼬样式选择字符串:

You can also use a Mongoose style selection string:

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

更多推荐

猫鼬选择字段(嵌套)

本文发布于:2023-11-22 13:30:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   字段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!