How can I get an array of all the doc ids in MongoDB? I only need a set of ids but not the doc contents.
相关问题
- MongoDB can not create unique sparse index (duplic
- Spring Data MongoDB - lazy access to some fields
- Golang mongodb aggregation
- How to convert from Timestamp to Mongo ObjectID
- MongoDB Indexing: Multiple single-field vs single
相关文章
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- mongodb error: how do I make sure that your journa
- How to track MongoDB requests from a console appli
- MongoError: cannot infer query fields to set, path
- Pymongo $in Query Not Working
- django.core.exceptions.ImproperlyConfigured: '
- How to represent an array with mixed types
You can do this in the Mongo shell by calling
map
on the cursor like this:The result is that
a
is an array of just the_id
values.The way it works in Node is similar.
(This is MongoDB Node driver
v2.2
, and Nodev6.7.0
)Remember to put
map
beforetoArray
as thismap
is NOT the JavaScriptmap
function, but it is the one provided by MongoDB and it runs within the database before the cursor is returned.I also was wondering how to do this with the MongoDB Node.JS driver, like @user2793120. Someone else said he should iterate through the results with .each which seemed highly inefficient to me. I used MongoDB's aggregation instead:
The sorting phase is optional. The match one as well if you want all the collection's _ids. If you console.log the result, you'd see something like:
Then just use the contents of result[0].ids somewhere else.
The key part here is the $group section. You must define a value of null for _id (otherwise, the aggregation will crash), and create a new array field with all the _ids. If you don't mind having duplicated ids (according to your search criteria used in the $match phase, and assuming you are grouping a field other than _id which also has another document _id), you can use $push instead of $addToSet.
I struggled with this for a long time, and I'm answering this because I've got an important hint. It seemed obvious that:
would be the answer.
It worked, sort of. It would find the first 101 documents and then the application would pause. I didn't let it keep going. This was both in Java using MongoOperations and also on the Mongo command line.
I looked at the mongo logs and saw it's doing a colscan, on a big collection of big documents. I thought, crazy, I'm projecting the _id which is always indexed so why would it attempt a colscan?
I have no idea why it would do that, but the solution is simple:
or in Java:
Then it was able to proceed along as normal, using stream style:
Mongo can do some good things and it can also get stuck in really confusing ways. At least that's my experience so far.
One way is to simply use the runCommand API.
which gives you something like this:
However, there's an even nicer way using the actual
distinct
API:which just gives you an array of ids:
Not sure about the first version, but the latter is definitely supported in the Node.js driver (which I saw you mention you wanted to use). That would look something like this:
i had a similar requirement to get ids for a collection with 50+ million rows. I tried many ways. Fastest way to get the ids turned out to be to do mongoexport with just the ids.
Another way to do this on mongo console could be:
Hope that helps!!!
Thanks!!!