I am using Mongoose in a MEAN environment. How can I make sure to not have any duplicate results in my result set? Example: my database contains 10 (partly duplicate) names:
我在MEAN环境中使用Mongoose。如何确保结果集中没有任何重复结果?示例:我的数据库包含10个(部分重复)名称:
- Allan
- Allan Fourier
- Allan
- Allan Maxwell
- Allan
- Allan Foo
- Allan Whatever
- Allan Whoever
- Allan Smith
- Allan Rogers
When querying this database for 'Allan' or maybe even just 'all' (using .find(regex...) and limiting the number of returned results to 5, I get this:
当查询这个数据库中的'Allan'或者甚至只是'all'(使用.find(正则表达式...)并将返回的结果数量限制为5时,我得到这个:
- Allan
- Allan Fourier
- Allan
- Allan Maxwell
- Allan
Having three duplicate entries of 'Allan', we waste a lot of result-diversity (talking about an autocomplete function for a search input field). I need the returned result set free of duplicates, such as:
有三个重复的'Allan'条目,我们浪费了很多结果多样性(谈论搜索输入字段的自动完成功能)。我需要返回的结果集没有重复项,例如:
- Allan
- Allan Fourier
- Allan Maxwell
- Allan Foo
- Allan Whatever
How can that be achieved using mongoose, if at all?
如果有的话,如何使用猫鼬实现这一目标?
3 个解决方案
#1
You can use find
to establish the query and then chain a call to distinct
on the resulting query object to get the unique names in the result:
您可以使用find来建立查询,然后在生成的查询对象上链接对distinct的调用,以获取结果中的唯一名称:
var search = 'Allan';
Name.find({name: new RegExp(search)}).distinct('name').exec(function(err, names) {...});
Or you can combine it all into a call to distinct
on the model, providing the query object as the second parameter:
或者,您可以将它全部组合到模型上的distinct上,将查询对象作为第二个参数:
var search = 'Allan';
Name.distinct('name', {name: new RegExp(search)}, function(err, names) {...});
In both cases, names
is an array of just the distinct names, not full document objects.
在这两种情况下,名称只是不同名称的数组,而不是完整的文档对象。
You can also do this with aggregate
which would then let you directly limit the number of results:
您也可以使用聚合执行此操作,然后可以直接限制结果数量:
Name.aggregate([
{$match: {name: new RegExp(search)}},
{$group: {_id: '$name'}},
{$limit: 5}
])
#2
You can use MongoDB's distinct()
query to find only distinct values (i.e., unique) in your set. Per the API docs, distinct can be used with Mongoose.
您可以使用MongoDB的distinct()查询来查找集合中的不同值(即唯一值)。根据API文档,distinct可以与Mongoose一起使用。
Their example:
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
With db.inventory.distinct( "dept" )
will return [ "A", "B" ]
使用db.inventory.distinct(“dept”)将返回[“A”,“B”]
#3
You can filter the search result which is array using method suggested here:
您可以使用此处建议的方法过滤搜索结果,即数组:
从Array中删除重复项
#1
You can use find
to establish the query and then chain a call to distinct
on the resulting query object to get the unique names in the result:
您可以使用find来建立查询,然后在生成的查询对象上链接对distinct的调用,以获取结果中的唯一名称:
var search = 'Allan';
Name.find({name: new RegExp(search)}).distinct('name').exec(function(err, names) {...});
Or you can combine it all into a call to distinct
on the model, providing the query object as the second parameter:
或者,您可以将它全部组合到模型上的distinct上,将查询对象作为第二个参数:
var search = 'Allan';
Name.distinct('name', {name: new RegExp(search)}, function(err, names) {...});
In both cases, names
is an array of just the distinct names, not full document objects.
在这两种情况下,名称只是不同名称的数组,而不是完整的文档对象。
You can also do this with aggregate
which would then let you directly limit the number of results:
您也可以使用聚合执行此操作,然后可以直接限制结果数量:
Name.aggregate([
{$match: {name: new RegExp(search)}},
{$group: {_id: '$name'}},
{$limit: 5}
])
#2
You can use MongoDB's distinct()
query to find only distinct values (i.e., unique) in your set. Per the API docs, distinct can be used with Mongoose.
您可以使用MongoDB的distinct()查询来查找集合中的不同值(即唯一值)。根据API文档,distinct可以与Mongoose一起使用。
Their example:
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
With db.inventory.distinct( "dept" )
will return [ "A", "B" ]
使用db.inventory.distinct(“dept”)将返回[“A”,“B”]
#3
You can filter the search result which is array using method suggested here:
您可以使用此处建议的方法过滤搜索结果,即数组:
从Array中删除重复项