我正在使用Node.js程序将数据插入MongoDB数据库。我已将数据插入名为“repl-failOver”的集合中。
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://localhost:30002/test", function(err, db) {
if (err) throw err;
db.collection("repl-failOver").insert( { "documentNumber" : document++}, function (err, doc) {
if (err) throw err;
console.log(doc);
});
db.close();
});
当我使用Mongo shell并使用show collections
列出数据库中的集合时,我能够看到集合“repl-failOver”。
如何从mongo shell为此集合运行find命令?
答案 0 :(得分:64)
使用以下语法:
db['repl-failOver'].find({})
或
db.getCollection('repl-failOver').find({})
您可以在手册的Executing Queries部分找到更多信息:
如果mongo shell不接受集合的名称,则为 实例,如果名称包含空格,连字符或以a开头 数字,您可以使用替代语法来引用集合,如 在下面:
db["3test"].find() db.getCollection("3test").find()
答案 1 :(得分:3)
您从访问具有特定字符(-
,_
,)的集合时收到此错误。我解释了解决方法here,但基本上你只需要做
db.getCollection("repl-failOver").insert(...)