简体   繁体   中英

MongoDB insert into collection through CLI

I have a mongodb running on my development machine. I need to insert some documents into few collections before deploying the web app.

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

But I receive error Syntax Error: missing ] in computed property name

How should I fix my insert script so that I can populate mongo db collections with data?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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