访问作为 mongodb 中集合项的数组元素
Access an array element that is a collection item in mongodb
前言:
使用 Express 创建一个 REST API,专门用于对 "scheduledReports" 下面的 mongo 用户集合项进行 CRUD 操作。
它是一个数组,用于存储在前端创建的报告对象。
集合中的每个用户以及属于该用户的任何报告都将有一个唯一的 ObjectId 作为标识符。
案例:用户想要更新其集合中的报告。
问题:PUT 请求需要先在集合中找到用户,然后才能找到用户想要操作的报表。搜索将基于它们的两个 ObjectId。如何在 Mongoose 中完成此操作?
Users Collection
{
"_id": ObjectId("54c7ed6c4aac70e63c6e8e3d")
"username" : "test",
"email" : "test@system.com",
"scheduledReports" : [
{
"_id" : ObjectId("54ea4d490d24155a73497dc4"),
"reportURI" : "report",
"frequency" : "hourly|daily|weekly|monthly",
"optionalMsg" : "optionalMsg",
"subject" : "subject",
"recipient" : "hugurlu@paypal.com",
"attachments" : [
"pdf|png|csv"
]
},
....
简化信息:利用 mongoose,使用其 ObjectID 访问 Users 集合中的用户,然后使用给定的 ObjectId 再次访问 "scheduledReports" 数组中的元素。
到达该元素后,将其中的值(ObjectId 除外)替换为新的更新对象。
您想要的可能是 positional operator。
如果user
是集合模型。假设您从 ob
中的前端获取对象 ID
//assuming you get the _id's from the front end in the ob object
//this is more useful when you have simple manipulation on the report
user.update({_id:ob.user_id,scheduledReports:ob.report_id},
{"scheduledReports.$":<your new obj>},function(err,doc){
});
//or
user.update({_id:ob.user_id,scheduledReports:ob.report_id},
{<update operator>:{"scheduledReports.$":<your new obj>}},function(err,doc){
});
// you can also use "scheduledReports.$.field" to update that field
或者另一种方法是
user.findOne({_id:ob.user_id, scheduledReports:ob.report_id},function(err,doc){
if(!err&&doc){
//doc.scheduledReports will have the array
//you need to manually search for the particular report id in that array
// and then manipulate
//This is more useful when you have complex manipulations.
}
});
如果第二个查询参数在引号中,并且给出了需要访问的字段,则有效。
User.update({username: userId, "scheduledReports._id": ob.report_id},
{$set{"scheduledReports.$": <new obj> }},function(err,doc){
//"scheduledReports.$.field" can be used to update that field
console.log(doc); //if operation is successful doc returns 1, otherwise 0
});
前言: 使用 Express 创建一个 REST API,专门用于对 "scheduledReports" 下面的 mongo 用户集合项进行 CRUD 操作。 它是一个数组,用于存储在前端创建的报告对象。 集合中的每个用户以及属于该用户的任何报告都将有一个唯一的 ObjectId 作为标识符。
案例:用户想要更新其集合中的报告。
问题:PUT 请求需要先在集合中找到用户,然后才能找到用户想要操作的报表。搜索将基于它们的两个 ObjectId。如何在 Mongoose 中完成此操作?
Users Collection
{
"_id": ObjectId("54c7ed6c4aac70e63c6e8e3d")
"username" : "test",
"email" : "test@system.com",
"scheduledReports" : [
{
"_id" : ObjectId("54ea4d490d24155a73497dc4"),
"reportURI" : "report",
"frequency" : "hourly|daily|weekly|monthly",
"optionalMsg" : "optionalMsg",
"subject" : "subject",
"recipient" : "hugurlu@paypal.com",
"attachments" : [
"pdf|png|csv"
]
},
....
简化信息:利用 mongoose,使用其 ObjectID 访问 Users 集合中的用户,然后使用给定的 ObjectId 再次访问 "scheduledReports" 数组中的元素。 到达该元素后,将其中的值(ObjectId 除外)替换为新的更新对象。
您想要的可能是 positional operator。
如果user
是集合模型。假设您从 ob
//assuming you get the _id's from the front end in the ob object
//this is more useful when you have simple manipulation on the report
user.update({_id:ob.user_id,scheduledReports:ob.report_id},
{"scheduledReports.$":<your new obj>},function(err,doc){
});
//or
user.update({_id:ob.user_id,scheduledReports:ob.report_id},
{<update operator>:{"scheduledReports.$":<your new obj>}},function(err,doc){
});
// you can also use "scheduledReports.$.field" to update that field
或者另一种方法是
user.findOne({_id:ob.user_id, scheduledReports:ob.report_id},function(err,doc){
if(!err&&doc){
//doc.scheduledReports will have the array
//you need to manually search for the particular report id in that array
// and then manipulate
//This is more useful when you have complex manipulations.
}
});
如果第二个查询参数在引号中,并且给出了需要访问的字段,则有效。
User.update({username: userId, "scheduledReports._id": ob.report_id},
{$set{"scheduledReports.$": <new obj> }},function(err,doc){
//"scheduledReports.$.field" can be used to update that field
console.log(doc); //if operation is successful doc returns 1, otherwise 0
});