MongoDB集合连字符名称 [英] MongoDB collection hyphenated name

查看:80
本文介绍了MongoDB集合连字符名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node.js程序将数据插入MongoDB数据库.我已将数据插入名为"repl-failOver"的集合中.

I'm using Node.js program to insert data into a MongoDB database. I have inserted data into a collection named "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".

When I use the Mongo shell and list down the collections in the database using show collections I am able to see the collection "repl-failOver".

如何从mongo shell中为该集合运行find命令?

How do I run a find command from the mongo shell for this collection?

推荐答案

使用以下语法:

db['repl-failOver'].find({})

db.getCollection('repl-failOver').find({})

您可以在执行查询中找到更多信息手册部分:

如果mongo shell不接受集合名称,则 名称包含空格,连字符或以."开头的实例 数字,您可以使用其他语法来引用该集合,例如 在以下内容中:

If the mongo shell does not accept the name of the collection, for instance if the name contains a space, hyphen, or starts with a number, you can use an alternate syntax to refer to the collection, as in the following:

db["3test"].find()

db.getCollection("3test").find()

这篇关于MongoDB集合连字符名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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