MongoDB 在 Node 中缺少一些方法 [英] MongoDB missing some methods in Node

查看:52
本文介绍了MongoDB 在 Node 中缺少一些方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Mongo 的新手,似乎在处理一些基本的事情时遇到了问题.

I'm new to Mongo, and seem to be having problems getting some basic things to work.

注释代码如下.短的是 db.getCollectionNames 是未定义的,还有 db.foo.find()db.foos.find()> 两者都炸掉了,即使代码中列出了一个确实有效的 foo 集合.

The annotated code is below. The short of it is that db.getCollectionNames is undefined, and also db.foo.find() and also db.foos.find() both bomb out, even though there is a foo collection listed from the code that actually does work.

我的 mongo 安装是否缺少某些功能?

Is my mongo install missing some features or something?

var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://localhost:27017/mongotest', function(err, db){
    try {
        db.collections(function(e, cols) {
            cols.forEach(function(col) {
                console.log(col.collectionName); //WORKS just fine
            });
        });
        console.log(typeof db.getCollectionNames); //undefined

        //db.foos.find(); //[TypeError: Cannot call method 'find' of undefined]
        //db.foo.find(); //[TypeError: Cannot call method 'find' of undefined]
    } catch (ex) {
        console.log(ex);
    } finally {
        setTimeout(function(){ db.close(); }, 2000);
    }
});

推荐答案

看起来您可能正在查看 Mongo shell 文档或旧版本的 node.js 本机驱动程序 API.

Looks like you may be looking at the Mongo shell documentation or an old version of the node.js native driver API.

在当前,2.x 版本驱动程序 getCollectionNames 被替换为 listCollections.

In the current, 2.x version of the driver, getCollectionNames is replaced by listCollections.

关于 db.foo.find() 语法,据我所知,本机驱动程序从未支持过.相反,那将是:

Regarding the db.foo.find() syntax, that's never been supported by the native driver as far as I know. Instead, that would be:

db.collection('foo').find()

这篇关于MongoDB 在 Node 中缺少一些方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆