MongoDB 通过 CLI 插入集合

MongoDB insert into collection through CLI

我的开发机器上有一个 mongodb 运行ning。在部署 Web 应用程序之前,我需要将一些文档插入到几个集合中。

在文档中,我使用此代码创建集合并将文档插入其中。

db.createCollection("usersTypes");
db.collection.insertMany({[
       { "_id":"1", "title":"Basic" },
       { "_id":"2", "title":"Premium" }
    ]},
    {
        writeConcern: "usersTypes",
        ordered: false
    });

为了 运行 包含插入语句的 JS 文件,我在命令行上使用此代码: mongo mydb statements.js

但是我收到错误消息Syntax Error: missing ] in computed property name

我应该如何修复我的插入脚本,以便我可以用数据填充 mongo 数据库集合?

尝试

> db.userTypes.insertMany([{ "_id":"1", "title":"Basic"},{ "_id":"2", "title":"Premium" }]);
{ "acknowledged" : true, "insertedIds" : [ "1", "2" ] }

您可以在 db 之后指定 collection 名称。

第一个参数不需要花括号(只需要一个文档数组)。

write concern 指定确认写入的彻底程度(和缓慢程度)