node.js 猫鼬填充在一个对象中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14594511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:03:21  来源:igfitidea点击:

Mongoose populate within an object?

node.jsmongoose

提问by Ryan Endacott

I'm not sure how to populate the sample schema below or if it is even possible. Can a reference be within an object like below? If you can, how would you populate it? E.g. .populate('map_data.location');?

我不确定如何填充下面的示例架构,或者是否可能。引用可以在像下面这样的对象内吗?如果可以,你会如何填充它?例如.populate('map_data.location');

var sampleSchema = new Schema({
  name: String,
  map_data: [{
    location: {type: Schema.Types.ObjectId, ref: 'location'},
    count: Number
  }]
});

Or will I have to have two separate arrays for location and count like so:

或者我是否必须有两个单独的数组用于位置和计数,如下所示:

// Locations and counts should act as one object. They should
// Be synced together perfectly.  E.g. locations[i] correlates to counts[i]
locations: [{ type: Schema.Types.ObjectId, ref: 'location'}],
counts: [Number]

I feel like the first solution would be the best, but I'm not entirely sure how to get it working within Mongoose.

我觉得第一个解决方案是最好的,但我不完全确定如何让它在 Mongoose 中工作。

Thank you very much for any help!

非常感谢您的帮助!

回答by Jed Watson

The first solution is possible.

第一种解决方案是可能的。

Mongoose currently has limitations (see this ticket here) populating multiple levels of embedded documents, however isvery good at understanding nested paths within a single document - what you're after in this case.

猫鼬目前有限制(在这里看到这张票)填充嵌入文档的多层次,不过在一个文档中了解嵌套的路径非常好-你在这种情况下,后在做什么。

Example syntax would be:

示例语法是:

YourSchema.find().populate('map_data.location').exec(...)

YourSchema.find().populate('map_data.location').exec(...)

Other features, such as specifying getters / setters on paths, orderBy and where clauses, etc. also accept a nested paths, like this example from the docs:

其他功能,例如在路径上指定 getter/setter、orderBy 和 where 子句等,也接受嵌套路径,如文档中的这个示例:

personSchema.virtual('name.full').get(function () {
  return this.name.first + ' ' + this.name.last;
});

Internally Mongoose splits the string at the dots and sorts everything out for you.

在内部 Mongoose 在点处拆分字符串并为您整理所有内容。

回答by kkochanski

First option is ok, but if someone would have problems with that query map_data.location- Mongoose returns empty array instead of object - I found that this will work:

第一个选项没问题,但是如果有人对该查询有问题map_data.location- Mongoose 返回空数组而不是对象 - 我发现这会起作用:

.populate({
     path: 'map_data.location',
     model: 'Location'
})

回答by Otis-iDev

YourSchema.find()
.populate(`{
path: 'map_data',
populate: {path: 'location' }
}`).exec(...)

The above statement will populate array of map_data and also location object of each map_data.

上面的语句将填充 map_data 数组以及每个 map_data 的位置对象。

hope this helps someone.

希望这有助于某人。

回答by Rodolfo Merlo Ali

i think you will need this:

我想你会需要这个:

if you schema is this:

如果你的架构是这样的:

var sampleSchema = new Schema({
name: String,
map_data: [{
location: {type: Schema.Types.ObjectId, ref: 'location'},
count: Number
}]
});

you monggosse query have to be:

你 monggosse 查询必须是:

const responseMap=await YourSchema.find()
.populate(
path: 'map_data._id',
model: 'location'
select:['nameLocation','geoPoints','count'],
).exec();
console.log(responseMap);

but you countvariable have to be in location schema, and map_data have to containt only id location.

但是您计算变量必须在位置模式中,而 map_data 必须仅包含 id 位置。

regards from Bolivia

来自玻利维亚的问候

回答by Daniel Martin Jimenez

The first option is the best. "count" is part of object "map_data".

第一种选择是最好的。“count”是对象“map_data”的一部分。