简体   繁体   中英

Mongoose + NodeJS

I have in my mongoDB more than 50000 entries with an ID (in one collection).

I would like to get only the ID (and not the other infos).

In server.js , I connect to mongoDB via mongoose:

mongoose.connect('mongodb://10.3.5.12/mydb');

I need to iterate all the 50 000 entries to check the ID, check the duplicates and store in a list.

Do I need to create a controller? It will be very heavy with 50 000 entries? Any idea how I can proceed?

var listId= [];
...
listId.push(id);

[EDIT] Could I try something like that with mongoose?

    MyModel.find().distinct('id', function(error, id) {
        // ids is an array of all ObjectIds
       var listId= [];
       ...
       listId.push(id);
    });

Thanks for your help!

If you're using Mongoose 3.x+ you can do this:

MyModel.find().distinct('_id', function(error, ids) {
    // ids is an array of all ObjectIds
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM