如何在数组中获取objectId的内容- Mongoose / NodeJs
How to get content of objectId in an Array - Mongoose / NodeJs
我想用我的objectId(通过用户id找到)的内容获得我的标签的数组/对象。
userModel
var userSchema = new mongoose.Schema({
pseudo : { type : String },
fullname : { type : String },
password : { type : String },
email : { type : String },
//tags : { type : Array },
tags : [
{
objectId : { type : mongoose.Schema.Types.ObjectId, ref: 'tag'},
completed : { type : Boolean }
}
],
dateCreation : { type : Date, default : Date.now }
});
tagModel
var tagSchema = new mongoose.Schema({
name : { type : String },
alias : { type : String },
description : { type : String },
dateCreation : { type : Date, default : Date.now }
});
<<p> 猫鼬请求/strong> userModel.find({ _id : req.session.passport.user._id }).select('tags').populate('objectId', 'name alias').lean().exec(function (err, result) {
console.log(JSON.stringify(result));
});
console.log
[
{
"_id":"538efe4a3bb9d97018b90ee4",
"tags":[
{
"objectId":"538f25f7f4d621281b7376e9",
"completed":false,
"_id":"538f25f7f4d621281b7376ea"
}
]
}
]
我想获得的内容objectId:/像'name', 'alias'…
我知道我可以写两个请求来获取内容,但它没有优化:/
你能帮我吗?
谢谢,
您需要提供要在populate
调用中填充的字段的完整点符号路径:
userModel.find({ _id : req.session.passport.user._id })
.select('tags')
.populate('tags.objectId', 'name alias')
.lean()
.exec(function (err, result) {
console.log(JSON.stringify(result));
}
);
相关文章:
- 如何检查用户是否已经在NodeJs-Mongoose中关注其他用户
- 无法读取未定义的属性 - NodeJS,Express,Mongoose
- Schema.pre in Mongoose modules (NodeJS)
- NodeJS, Mongoose, return variable
- 使用 nodejs、restify 和 mongoose 确保数据完整性
- nodejs express mongoose connect to db error
- 使用$http.get(mongoose with nodejs)在嵌套的JSON中获取数组
- 如何从nodejs中的find返回,mongoose mongodb集合
- NodeJS+Mongoose连接超时
- Mongoose,nodejs,全文搜索
- NodeJs、AngularJs、Mongoose给出错误:TypeError:Object.keys在Function
- 在NodeJS中使用Mongoose+Mockgoose更新时忽略了唯一索引
- 使用nodejs、mongoose和JSON对象数组拼接
- 使用Mongoose查询是正确的,但是forEach之后数组总是空的.Nodejs / MongoDB
- NodeJs Mongoose循环收集数据
- Mongoose不会用NodeJs保存任何东西到db
- 仅在nodejs上使用mongoose列出所有GridFs元数据
- NodeJS, MongoDB, Mongoose保存大数据非阻塞
- 如何在数组中获取objectId的内容- Mongoose / NodeJs
- Mongoose + NodeJS -遍历文档