如何从数组中删除重复的条目?
在下面的例子中,"C++中的算法"被添加两次.
$ unset修饰符删除特定字段但如何从字段中删除条目?
> db.users.find()
{ "_id" : ObjectId("4f6cd3c47156522f4f45b26f"),
"favorites" : { "books" : [ "Algorithms in C++",
"The Art of Computer Programmning",
"Graph Theory",
"Algorithms in C++" ] },
"name" : "robert" }
Run Code Online (Sandbox Code Playgroud)
kyn*_*nan 32
由于MongoDB的2.2可以使用聚合框架用$unwind,$group和$project舞台来实现:
db.users.aggregate([{$unwind: '$favorites.books'},
{$group: {_id: '$_id',
books: {$addToSet: '$favorites.books'},
name: {$first: '$name'}}},
{$project: {'favorites.books': '$books', name: '$name'}}
])
Run Code Online (Sandbox Code Playgroud)
请注意,需要$project重命名favorites字段,因为$group聚合字段不能嵌套.
最简单的解决方案是使用setUnion(Mongo 2.6+):
db.users.aggregate([
{'$addFields': {'favorites.books': {'$setUnion': ['$favorites.books', []]}}}
])
Run Code Online (Sandbox Code Playgroud)
另一个(更冗长的)版本基于@kynan的答案,但是保留了所有其他字段而未明确指定它们(Mongo 3.4+):
> db.users.aggregate([
{'$unwind': {
'path': '$favorites.books',
// output the document even if its list of books is empty
'preserveNullAndEmptyArrays': true
}},
{'$group': {
'_id': '$_id',
'books': {'$addToSet': '$favorites.books'},
// arbitrary name that doesn't exist on any document
'_other_fields': {'$first': '$$ROOT'},
}},
{
// the field, in the resulting document, has the value from the last document merged for the field. (c) docs
// so the new deduped array value will be used
'$replaceRoot': {'newRoot': {'$mergeObjects': ['$_other_fields', "$$ROOT"]}}
},
// this stage wouldn't be necessary if the field wasn't nested
{'$addFields': {'favorites.books': '$books'}},
{'$project': {'_other_fields': 0, 'books': 0}}
])
{ "_id" : ObjectId("4f6cd3c47156522f4f45b26f"), "name" : "robert", "favorites" :
{ "books" : [ "The Art of Computer Programmning", "Graph Theory", "Algorithms in C++" ] } }
Run Code Online (Sandbox Code Playgroud)
你所要做的就是使用 MapReduce 来检测和计算重复标签..然后使用它$set来替换整个书籍{ "_id" : ObjectId("4f6cd3c47156522f4f45b26f"),
这已经在这里讨论了七次..请参阅
http://csanz.posterous.com/look-for-duplicates-using-mongodb-mapreduce
http://www.mongodb.org/display/DOCS/MapReduce
| 归档时间: |
|
| 查看次数: |
20151 次 |
| 最近记录: |