简体   繁体   English

$pull 不适用于 mongoose (MongoDB) 中的数组

[英]$pull is not working for array in mongoose (MongoDB)

In mongoDB (Through mongoose), I am trying to remove an element in array of a collection and using $pull operator for that, but it is not working.在 mongoDB(通过 mongoose)中,我试图删除集合数组中的一个元素并为此使用 $pull 运算符,但它不起作用。

Rooms collection in mongoDB mongoDB 中的房间集合

{
"_id":"sampleObjectId",
"users": ["email1@example.com", "email2@example.com"]
}

Backend code to remove a user (mongoose using Node.js)删除用户的后端代码(使用 Node.js 的 mongoose)

Rooms.update(
              { _id: "sampleRoomId" },
              {
                $pull: {
                    users: "email1@exmple.com" }
                },
              }
            )

Noting happens to collection on running this code, no change happens.注意在运行此代码时收集,没有发生任何变化。 code runs with no errors and in output it says "nModified":0 .代码运行时没有错误,并且在输出中显示"nModified":0 I have no idea how to remove a user from collection.我不知道如何从集合中删除用户。

//actual code output from the mongo shell. This is working as expected. check if you given //correct value in object_id query parameter.

> db.rooms.find();
{ "_id" : "sampleObjectId", "users" : [ "email1@example.com", "email2@example.com" ] }
> db.rooms.update(
... {_id: "sampleObjectId"},
... {$pull:{users:"email1@example.com"}}
... );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.rooms.find();
{ "_id" : "sampleObjectId", "users" : [ "email2@example.com" ] }
> db.version();
4.2.6
>

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

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