After connecting to the database, you can create a database and write data.
Creating a Database
The syntax format for creating a database in MongoDB is as follows:
use DATABASE_NAME
Create a database named myfirstdb and insert a document.
> use myFirstDBswitched to db myFirstDB> db.myFirstDB.insert({"test":"myFirstDB"})WriteResult({ "nInserted" : 1 })
Query the created database.
> show dbsadmin 0.000GBconfig 0.000GBlocal 0.041GBmyFirstDB 0.000GB
Creating a collection
In MongoDB, the createCollection() method is used to create a collection.
Syntax format:
db.createCollection(name, options)
Parameter description:
name: Name of the collection to be created.
options: Optional parameters specifying options related to memory size and indexes.
Options Field | Type | Description |
capped | BOOL | Indicates whether to set the maximum byte size of the collection. If true, the size parameter must be set. Default is false. |
autoIndexId | BOOL | Set whether to automatically create an index. If true, an index is automatically created on the _id field. Default is false. |
size | Value | Set the maximum number of bytes for the collection. |
max | Value | Set the maximum number of documents in the collection. |
Example of creating the firstcol collection in the myfirstdb database:
> use myFirstDBswitched to db myFirstDB> db.createCollection("FirstCol"){"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1634821900, 2),"signature" : {"hash" : BinData(0,"WFu7yj8wjeUBWG3b+oT84Q8wIw8="),"keyId" : NumberLong("6990600483068968961")}},"operationTime" : Timestamp(1634821900, 2)}
View the created collection:
> show collectionsFirstCol
Create the firstcol collection with a maximum size of 6142800B and a maximum of 10000 documents, example as follows:
> db.createCollection("FirstCol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ){"note" : "the autoIndexId option is deprecated and will be removed in a future release","ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1634821879, 1),"signature" : {"hash" : BinData(0,"EuIbp2fu9Yh38HOBHLgYqljdKaE="),"keyId" : NumberLong("6990600483068968961")}},"operationTime" : Timestamp(1634821879, 1)}
Inserting Documents
MongoDB uses the insert() or save() method to insert documents into the collection, example as follows:
> db.FirstCol.insert({name:"Li Si",sex:"Female",age:25,status:"A"})WriteResult({ "nInserted" : 1 })
Viewing Inserted Documents in the Collection:
> db.FirstCol.find(){ "_id" : ObjectId("61716957a6fe1ef4d7eae979"), "name" : "Li Si", "sex" : "Female", "age" : 25, "status" : "A" }
db.collection.insertMany() is used to insert one or more documents into the collection. The syntax format is as follows:
db.collection.insertMany([ <document 1> , <document 2>, ... ])
For example:
> db.FirstCol.insertMany([{name:"Li San",sex:"Female",age:25,status:"A"},{name:"Wang Liu",sex:"Male",age:26,status:"B"},{name:"Harry Smith",sex:"Male",age:26,status:"A",groups:["news","sports"]}]){"acknowledged" : true,"insertedIds" : [ObjectId("617282a3a4bb72d733b5c6d7"),ObjectId("617282a3a4bb72d733b5c6d8"),ObjectId("617282a3a4bb72d733b5c6d9")]}
Updating the Database
MongoDB uses update() to update documents in the collection.
Example of updating data in the FirstCol collection where name is Li San:
> db.FirstCol.update({name:"Li San", sex:"Female", age:25, status:"A"}, {$set:{'age':28}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Query modification results:
> db.FirstCol.find().pretty(){"_id" : ObjectId("618904b6258a6c38daf13abd"),"name" : "Li San","sex" : "Female","age" : 28,"status" : "A"}{"_id" : ObjectId("618904b6258a6c38daf13abe"),"name" : "Wang Liu","sex" : "Male","age" : 26,"status" : "B"}{"_id" : ObjectId("618904b6258a6c38daf13abf"),"name" : "Wang Wu","sex" : "Male","age" : 26,"status" : "A","groups" : ["news","sports"]}
Delete Database
MongoDB uses remove() to delete documents in the collection. Example:
> db.FirstCol.remove({name:"Li San",sex:"Female",age:28,status:"A"})WriteResult({ "nRemoved" : 1 })
Query delete results:
> db.FirstCol.find().pretty(){"_id" : ObjectId("618904b6258a6c38daf13abe"),"name" : "Wang Liu","sex" : "Male","age" : 26,"status" : "B"}{"_id" : ObjectId("618904b6258a6c38daf13abf"),"name" : "Wang Wu","sex" : "Male","age" : 26,"status" : "A","groups" : ["news","sports"]}