简体   繁体   中英

How to get all collections in mongodb

my query is like this

var db = new Db('NodeZMQ', new Server('192.168.3.110', 27017, { auto_reconnect: true }));

db.collection('clsSession', function (err, collection)
{
    collection.findOne({ "code": { "$in": ["eventCode"] }, "Venue.allowSales": true, "status": "O", "Venue.companyCode": "companyCode" }, function (err, document)
    {
        console.log(document);
    });
});

It gives one document. Actually it has lot of documents.. Since i am using findOne it is like that.. how to get all matching documents ? I am using node.js and mongodb. I am new to mongoDB and node.js.

Use collection.find( instead of collection.findOne( . This returns a cursor object you can use to iterate over the results.

The cursor is an object with many methods. Those you will find most helpful are the methods .hasNext() which checks if there are still unprocessed documents and .next() which gives you the next document.

var db = new Db('NodeZMQ', new Server('192.168.3.110', 27017, { auto_reconnect: true }));

db.collection('clsSession', function (err, collection)
{
collection.find({ "code": { "$in": ["eventCode"] }, "Venue.allowSales": true, "status":    "O", "Venue.companyCode": "companyCode" }).toArray(function (err, document)
{
    //here document is an array contains all the matching reocrds
    console.log(document);
});
});

This example based on assumption that you are using mongo-native driver but it helps to solve your problem.

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