简体   繁体   English

MongoDB通过CLI插入集合

[英]MongoDB insert into collection through CLI

I have a mongodb running on my development machine. 我的开发机器上正在运行mongodb。 I need to insert some documents into few collections before deploying the web app. 在部署Web应用程序之前,我需要将一些文档插入几个集合中。

As in documentation, I use this code to create collection and to insert documents into it. 就像在文档中一样,我使用此代码创建集合并将文档插入其中。

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

To run the JS file that contains the insert statements I use this code on the command line: mongo mydb statements.js 要运行包含插入语句的JS文件,我在命令行上使用以下代码: mongo mydb statements.js

But I receive error Syntax Error: missing ] in computed property name 但是我Syntax Error: missing ] in computed property name收到错误Syntax Error: missing ] in computed property name

How should I fix my insert script so that I can populate mongo db collections with data? 我应该如何修复插入脚本,以便可以用数据填充mongo db集合?

try 尝试

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

you can specify the collection name after db . 您可以在db之后指定集合名称。

you don't need curly braces around the first argument (just an array of documents is fine). 您不需要在第一个参数周围使用花括号(只需一个文档数组即可)。

write concern specifies how thoroughly (and slowly) writes are acknowledged 写入关注点指定如何(彻底)确认写入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM