Node.js: Mongodb db.collection.find () not working while collection.insert is running

I use node.js / express and I have Mongodb to store some datasets. On the web page, the user can enter, edit and delete data (everything works fine). For example, to add data, I have the following code:

 router.post('/addset', function(req,res) {
    var db = req.db;
    var collection = db.get('paramlist');
    collection.insert(req.body, function(err, result){
        res.send(
            (err === null) ? { msg: '' } : { msg: err }
        );
    });
});

In my app.js file I include lines

// Database
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/paramSet1');

and

app.use(function(req,res,next){
    req.db = db;
    next();
});

to make the database accessible in the rest of the code (following this guide: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/ , I start with these things).

, . : , . MongoDB, , ? collection.find.limit(1).size(),

undefined is not a function

. (router.post) var...

var findValue = collection.find({name:req.body.name});

console.log(findValue), JSON. console.log(findValue.next()), (undefined ).

collection.find({name:req.body.name}).limit(1)

collection.find({name:req.body.name}).limit(1).size()

. , , collection.insert, collection.update collection.remove , find() . , , .

.

: console.log(findValue):

    { col:
   { manager:
      { driver: [Object],
        helper: [Object],
        collections: [Object],
        options: [Object],
        _events: {} },
     driver:
      { _construct_args: [],
        _native: [Object],
        _emitter: [Object],
        _state: 2,
        _connect_args: [Object] },
     helper: { toObjectID: [Function], isObjectID: [Function], id: [Object] },
     name: 'paramlist',
     col:
      { _construct_args: [],
        _native: [Object],
        _emitter: [Object],
        _state: 2,
        _skin_db: [Object],
        _collection_args: [Object],
        id: [Object],
        emitter: [Object] },
     options: {} },
  type: 'find',
  opts: { fields: {}, safe: true },
  domain: null,
  _events: { error: [Function], success: [Function] },
  _maxListeners: undefined,
  emitted: {},
  ended: false,
  success: [Function],
  error: [Function],
  complete: [Function],
  resolve: [Function],
  fulfill: [Function],
  reject: [Function],
  query: { name: 'TestSet1' } }
+4
4

find cursor, . : findOne:

collection.findOne({name:req.body.name}, function(err, doc) {
    if (doc) {
        // A doc with the same name already exists
    }
});
+3

, , - . . :

collection.find({name:req.body.name}).limit(1).size();
0

http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/,

collection.find({name:req.body.name}).limit(1)

collection.find({name:req.body.name},{limit:1})

,

collection.find({name:req.body.name},{limit:1,project:{a:1},skip:1,max:10,maxScan:10,maxTimeMS:1000,min:100})

http://mongodb.imtqy.com/node-mongodb-native/2.0/api/Cursor.html

0
source

you can use like that

app.get('/', (req, res) => {
  db.collection('quotes').find().toArray((err, result) => {
    console.log(result);
    })
})
0
source

All Articles