【问题标题】:Access an array element that is a collection item in mongodb访问一个数组元素,它是 mongodb 中的集合项
【发布时间】:2015-04-28 01:16:27
【问题描述】:

前言: 使用 Express 创建一个 REST API,该 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"
        ]
    }, 
    ....

简化信息:利用猫鼬,使用其 ObjectID 访问用户集合中的用户,然后使用给定的 ObjectId 再次访问“scheduledReports”数组中的元素。 到达该元素后,将其中的值(ObjectId 除外)替换为新的更新对象。

【问题讨论】:

  • 添加更多关于你想要什么的信息。你的问题不够清楚。

标签: node.js mongodb express mongoose


【解决方案1】:

你想要的可能是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.
  }
});

【讨论】:

  • 用户和报告 ID 都是从前端发送的。但是,在更新数组元素时,mongose 会忽略 Object id 吗? PS:添加了简化信息。
  • 我正在尝试第一种方法,此时它给出了语法错误 {scheduledReports.$: 所以 ide 标记冒号并说 , or ) 预期。
  • 高级方法的另一个语法错误。
  • @hugurlu 编辑了方法。立即查看
【解决方案2】:

如果第二个查询参数在引号中,并且需要访问的字段被给出,则它可以工作。

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
});

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 2014-01-04
    • 2021-03-16
    • 1970-01-01
    • 2021-02-14
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多