本文介绍了node.js:当collection.insert工作时,Mongodb db.collection.find()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js/express,并且我有一个Mongodb来存储一些数据集.用户可以在网页上输入,编辑和删除数据(一切正常).例如,要添加数据,我具有以下代码:

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:

 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 }
        );
    });
});

在我的app.js文件中,包括以下行

In my app.js file I include the lines

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

以及

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

使数据库在其余代码中可访问(遵循本教程: http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/,我是这些方面的初学者).

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

因此,所有这些工作正常.我的问题如下:我想添加一个测试,如果数据库中已经存在具有相同名称的数据集,并向用户发送消息.遵循此答案如何查询MongoDB以测试项目是否存在吗?我尝试使用collection.find.limit(1).size(),但出现错误

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 How to query MongoDB to test if an item exists? I tried using collection.find.limit(1).size() but I get the error

undefined is not a function

我尝试了以下操作.在上述费用(router.post)中,我尝试在行var集合之后添加...

I tried the following things. In the cost above (router.post) I tried adding after the line var collection...

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

如果我随后执行console.log(findValue),我将得到一个巨大的输出JSON.然后,我尝试了console.log(findValue.next()),但遇到相同的错误(未定义不是函数).我也尝试过

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

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

以及

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

,但也会收到此错误.因此,总而言之,collection.insert,collection.update和collection.remove都可以​​工作,但find()不能.另一方面,当我进入mongo shell时,该命令运行正常.

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.

对于任何提示和想法,我将不胜感激.

I would be grateful for any hints and ideas.

console.log(findValue)的输出是:

The output to console.log(findValue) is:

    { 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' } }

推荐答案

find返回光标,而不是匹配的文档本身.但更适合您的情况是使用 :

find returns a cursor, not the matching documents themselves. But a better fit for your case would be to use findOne:

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

这篇关于node.js:当collection.insert工作时,Mongodb db.collection.find()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:42