title: node.js: Mongodb db.collection.find() not working while collection.insert works
body: <p>I'm using node.js/express and I have a Mongodb to store some sets of data. On a webpage the user can enter, edit and delete data (which all works fine). For example, to add data I have the following code:</p>
  
  <pre><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 }
          );
      });
  });
  </code></pre>
  
  <p>In my app.js file I include the lines</p>
  
  <pre><code>// Database
  var mongo = require('mongodb');
  var monk = require('monk');
  var db = monk('localhost:27017/paramSet1');
  </code></pre>
  
  <p>as well as</p>
  
  <pre><code>app.use(function(req,res,next){
      req.db = db;
      next();
  });
  </code></pre>
  
  <p>to make the database accessible in the rest of the code (following this tutorial: <a href="http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/" rel="nofollow noreferrer">http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/</a> , I'm a beginner with these things).</p>
  
  <p>So all this works fine. My problem is the following: I would like to add a test if a dataset with the same name is already in the database and give a message to the user. Following this answer <a href="https://stackoverflow.com/questions/8389811/how-to-query-mongodb-to-test-if-an-item-exists">How to query MongoDB to test if an item exists?</a> I tried using collection.find.limit(1).size() but I get the error </p>
  
  <pre><code>undefined is not a function
  </code></pre>
  
  <p>I tried the following things. In the cost above (router.post) I tried adding after the line var collection...</p>
  
  <pre><code>var findValue = collection.find({name:req.body.name});
  </code></pre>
  
  <p>If i then do console.log(findValue), I get a huge output JSON. I then tried console.log(findValue.next()) but I get the same error (undefined is not a function). I also tried</p>
  
  <pre><code>collection.find({name:req.body.name}).limit(1)
  </code></pre>
  
  <p>as well as</p>
  
  <pre><code>collection.find({name:req.body.name}).limit(1).size()
  </code></pre>
  
  <p>but also get this error. So in summary, collection.insert, collection.update and collection.remove all work, but find() does not. On the other hand, when I enter the mongo shell, the command works fine. </p>
  
  <p>I would be grateful for any hints and ideas. </p>
  
  <p>Edit: 
  The output to console.log(findValue) is:</p>
  
  <pre><code>    { 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' } }
  </code></pre>