简体   繁体   English

如何在mongoose中查询ObjectIds数组?

[英]How to Query an array of ObjectIds in mongoose?

I want to Update one variable of an object in array Inside my document. 我想更新数组中一个对象的一个​​变量在我的文档中。 I have the events document and Users array attached to that event. 我有事件文档和附加到该事件的Users数组。 So I have a particular UserId. 所以我有一个特定的UserId。 So firstly, Is it Possible to find that user inside the array of Users with Mongoose query? 首先,是否可以在具有Mongoose查询的Users数组中找到该用户? Because I have events document and inside I have User's array.After finding that User, I want to update its one variable. 因为我有事件文档,里面有User的数组。在找到User之后,我想更新它的一个变量。

exports.disableUsers = (req, res, next) => {
  let User;
  users: req.body.users; ///users Array
  for (var i = users.length - 1; i >= 0; i--) {
    if (users[i] == req.params.id1) {
      User = users[i]; ////Particular User
      break;
    }
  }
  Event.updateOne(
    { _id: req.params.id },
    {
      ///don't Know how to Update??
    }
  )
    .then(result => {
      console.log(result);
      if (result.n > 0) {
        res.status(200).json({ message: "Verification successful!" });
      } else {
        res.status(401).json({ message: "Not authorized!" });
      }
    })
    .catch(error => {
      res.status(500).json({
        message: "Verifying events failed!"
      });
    });
};

Above is my disableusers Function. 以上是我的disableusers功能。 So can anyone please tell how to find and Update a particular user in Array of Objects Inside the event Document. 所以任何人都可以告诉如何在事件文档内的对象数组中查找和更新特定用户。 And My eventSchema looks like this: 我的eventSchema看起来像这样:

const eventSchema = mongoose.Schema({
  title: { type: String, required: true },
  content: { type: String, required: true },
  imagePath: { type: String, required: true },
  startDate: { type: Date, required: true },
  endDate: { type: Date, required: true },
  category: { type: String, required: true },
  is_private : { type: Boolean},
  max_users: {type: Number},
  eventFee: {type: Number},
  event_manager: [{ name: String, phone: String, email: String }],
  address: [{ street: String, city: String, state: String, country: String, zipcode: String }],
  creator: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
  verify:{type:Boolean,required:true},
  users:[{id:String,email:String,PhoneNum:String,firstName:String,lastName:String,city:String,disable:{type:Boolean,default:false}}]
});

This would be much easier if you separated users in to its own collection, then you could just update using the user._id 如果您将用户分隔到自己的集合中,那么这将更容易,然后您可以使用user._id进行更新

However in your current case, it can be done like this if using mongo 3.6+ 但是在目前的情况下,如果使用mongo 3.6+,可以这样做

this will update user.disable to true also allows you to update multiple users in one query 这会将user.disable更新为true,还允许您在一个查询中更新多个用户

Event.update(
   { _id: req.params.id},
   { $set: { "users.$[user].disable" : true } },
   { 
     multi: true,
     arrayFilters: [ { "user.<Primary Key To Identify User>": { $in:<Array of values to match to the primary key on user> } } ]
   }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM