在mongodb / mongoose中使用$ addToSet之前,如何检查其他数组是否包含我想要添加的值?

时间:2017-11-11 05:37:41

标签: mongodb mongoose mongodb-query

我想检查其他'套'是否具有我想要添加的值。如果另一个集合具有该值,则将其从该集合中删除并将其添加到新集合中。{ "_id": "5a052785aa06c22429717bad", "word": "wordtest5", "__v": 0, "pronunciations": [ { "pronunciation": "wordtest1", "_id": "5a052785aa06c22429717bae", "likes": [ "remove_this", "5a0508cfcf4f6620786a3cb1" ], "rating": 1 }, { "pronunciation": "wordtest5", "_id": "5a0674c1053ae929db3a576c", "likes": [], "rating": 1 }, { "pronunciation": "testing5", "_id": "5a06770346be3d2ac6f31561", "likes": [ "remove_this", "5a0508cfcf4f6620786a3cb1", ], "rating": 1 } ] }

我想删除每个'likes'数组中的所有'remove_this'字符串。

1 个答案:

答案 0 :(得分:0)

在您提及的其他“套装”上使用 $ pull ,并在要添加的实际套装上使用 $ addToSet 。这样你就可以达到你想要的效果。

下次请非常清楚你的问题。