mongodb中存储数组和新数组的区别
我是 mongodb 的新手。
我在 mongodb 中存储了以下数据
{ "_id" : ObjectId("4"), "uid" : "1", "friendsIds" : [ "2", "3" ] }
情况 1: 现在,我有一组新的 FriendsIds ["2","3","4"]
我想比较这两个集合,并获取未存储在数据库中的值。在本例中,我想要得到 4
情况 2: 现在,我有一组新的 FriendsIds ["3","4"]
我想比较这两个集合,并获取存储在数据库中但不存储在新集合中的值。在这种情况下,我想得到2
情况2可以使用mapReduce完成吗?
mongodb中这两种情况如何实现?
I am new to mongodb.
I have the following data stored in mongodb
{ "_id" : ObjectId("4"), "uid" : "1", "friendsIds" : [ "2", "3" ] }
Case 1:
Now, I have new set of friendsIds ["2","3","4"]
I want to compare the both the sets, and get the value which is not stored in database. In this case, I want to get 4
Case 2:
Now, I have new set of friendsIds ["3","4"]
I want to compare the both the sets, and get the value which is stored in database but not in the new set. In this case, I want to get 2
Can the Case 2 be done using mapReduce?
How to achieve both the cases in mongodb?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于情况 1,您似乎需要
[$addToSet][1]
。看一下$each
示例。对于情况2,没有这样的服务器端功能。只需带回文档并在客户端进行比较即可。
您正在做什么操作,您只想要
2
而不是其他一切?For case 1, it looks like you need
[$addToSet][1]
. Take a look at the$each
example.For case 2, there is no such server-side function. Just bring back the document and compare client-side.
What action are you doing that you just want the
2
and not everything else?如果 ruby 中有两个数组
:
if you have two arrays
in ruby :
这可以在聚合框架中使用其 集合运算符 来完成:
This can be done in the aggregation framework using its set operators: