37
loading...
This website collects cookies to deliver better user experience
$ mongo
$ db
$ show dbs
$ use mydb
$ db.createCollection()
$ db.createCollection( <name>, {...options})
$ db.users.insertMany([
{
firstName: "Alex",
lastName: "Balkin"
},
{
firstName: "Tom",
lastName: "Xu"
}
])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("60d832029f3f6108a53af686"),
ObjectId("60d832029f3f6108a53af687")
]
}
insertMany([{...}, ...])
operation will create both the database mydb
and users
collection if they are not already exist.$ db.users.find({})
{ "_id" : ObjectId("60d832029f3f6108a53af686"), "firstName" : "Alex", "lastName" : "Balkin" }
{ "_id" : ObjectId("60d832029f3f6108a53af687"), "firstName" : "Tom", "lastName" : "Xu" }
$ db.users.find({}).pretty()
{
"_id" : ObjectId("60d832029f3f6108a53af686"),
"firstName" : "Alex",
"lastName" : "Balkin"
}
{
"_id" : ObjectId("60d832029f3f6108a53af687"),
"firstName" : "Tom",
"lastName" : "Xu"
}
$ db.users.find({
_id: ObjectId("60d832029f3f6108a53af686")
}).pretty()
{
"_id" : ObjectId("60d832029f3f6108a53af686"),
"firstName" : "Alex",
"lastName" : "Balkin"
}
$ db.users.find({
_id: ObjectId("60d832029f3f6108a53af686")
}, {firstName: 1, _id: 0 }).pretty()
{ "firstName" : "Alex" }
_id
filed then we have to specify it in the projection document the default behavior of the _id
field does not exclude from the query projection.We are done today, we will continue tomorrow. Happy Learning 🎉.