开发者

Distinct results in db.collection.find() query

开发者 https://www.devze.com 2023-04-07 01:43 出处:网络
I have query over a collection db.users.find(开发者_开发百科) which is returning duplication results. For example user._id \"1\" can repeat multiple times.

I have query over a collection db.users.find(开发者_开发百科) which is returning duplication results. For example user._id "1" can repeat multiple times.

Is there a way to return distinct results?


If you want only all the distinct user._id, use db.users.distinct("_id")

if you want the whole records with distinct _id you have to think of a strategy to choose between 2 records with the same user._id You can use group or map reduce but you have to think about, what do I want when there are 2 user with the same _id.

BTW, _id are usually generated by mongodb and there supposed to be unique. If you have 2 ids which are the same either you have a very very high insertion rate in your collection either you are generating _id yourself. Is there any particular reason you are generating non unique _id ?


A single find should never return duplicate results, as there's no such thing as a join in Mongo so there's no circumstance in which a single document would be returned twice by a query. So what you're describing sounds like it should never happen - but it's hard to say without more details.

However, one possibility is that find returns an open cursor, so if, for example, you were iterating through a large number of documents and updating them as you go, you may end up getting the same document back again later. The reason for this is that your update can increase the size of the document so that it no longer fits in the space it has, so it has to be reallocated space at the end of the collection, where it is later picked up again by the cursor. If that is what you're doing you probably need to think about doing the update in a different way.

0

精彩评论

暂无评论...
验证码 换一张
取 消