[英]How to make a mongo query to find all documents where an array contains a given id?
I am not sure what i am doing wrong.我不确定我做错了什么。 I have a User collection which has a field "followingUsers" (array).
我有一个用户集合,其中有一个字段“followingUsers”(数组)。 This array contains the id's of other users.
该数组包含其他用户的 ID。 When an user gets deleted i want that every other user document no longer has a reference to the deleted user in their followUsers array.
当用户被删除时,我希望每个其他用户文档不再在其 followUsers 数组中引用已删除的用户。 I am not experienced with mongo so i decided to make a find query first, but i get back an empty result on each attempt.
我对 mongo 没有经验,所以我决定先进行查找查询,但每次尝试都会返回一个空结果。
I wrote the following queries and none of them seem to work:我写了以下查询,但似乎都不起作用:
db.users.find({"followingUsers": "63d0522c655fa263cfe003e7"})
db.users.find({"followingUsers": {"$all": ["63d0522c655fa263cfe003e7"] } } )
In the image you can see the data i expect to get back: Data i expect to get back在图像中你可以看到我希望得到的数据: Data i expect to get back
What am i doing wrong?我究竟做错了什么?
With the help of the user with nickname ray, i found the correct queries:在昵称 ray 的用户的帮助下,我找到了正确的查询:
db.users.find( {'followingUsers': ObjectId("63cff74e3ae933124ee20d05")} )
db.users.find( {'followingUsers': {"$all":[ ObjectId("63cff74e3ae933124ee20d05") ] }} )
I was indeed storing ObjectId's inside the array and not just strings.我确实将 ObjectId 存储在数组中,而不仅仅是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.