[mongodb] Introduction

Time:2021-10-19

summary

Mongodb is a database based on distributed file storage. Written in C + +. It aims to provide scalable high-performance data storage solutions for web applications.

Mongodb is a product between relational database and non relational database. It is the most functional and relational database among non relational databases

Mongodb uses bson structure as storage and network storage. Bson is a binary JSON, which is extended on the basis of JSON and supports more types than JSON, such as date and bindata

The bson structure has the following characteristics

  • Lightweight
  • Ergodic
  • Efficiency

Document: a record stored in mongodb in bson structure, equivalent to row
Collection: a collection of documents, equivalent to a table
Database: a collection of collections, indexes, and other information

 

configuration file

mongod.cfg

storage:
  #Data storage location
  dbPath: F:\Database\Mongodb\Data
  journal:
    #Persistence
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  #Record form of log
  destination: file
  #Append
  logAppend: true
  #Log file (note file)
  path:  F:\Database\Mongodb\Log\mongod.log

# network interfaces
net:
  #Binding port
  port: 27017
  #Bind IP
  bindIp: 127.0.0.1

 

 

Start mongodb

one   Start by profile

mongod -config mongod.cfg

two   Start with specified parameters

mongod --dbpath "F:\Database\Mongodb\Data" --logpath "F:\Database\Mongodb\Log\mongod.log"

three   Installation services

mongod --dbpath "F:\Database\Mongodb\Data" --logpath "F:\Database\Mongodb\Log\mongod.log" --serviceName "mongodb" --serviceDisplayName "mongodb" --install

 

Connect mongodb

CMD execution

one   Default configuration

mongo

two   Connection string

mongo mongodb://localhost

 

Crud operation

Create

one   Insert a single document

db.logs.insertOne()

Var data = {"userid": 10, "operate": "login", "createtime": new date()};
db.logs.insertOne(data);

return

{
    "Acknowledged": true, // whether the writing is successful
    "Insertedid": objectid ("5e929706fe1792ce954f65f1") // insert the ID of the line
}

 

two   Insert multiple documents

var data = [
    {"userid": 10, "operate": "login", "createtime": new date()},
    {"userid": 10, "operate": "click home", "createtime": new date()},
    {"userid": 10, "operate": "view list", "createtime": new date()}
]
db.logs.insertMany(data);

return

{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("5e9297dbfe1792ce954f65f2"),
                ObjectId("5e9297dbfe1792ce954f65f3"),
                ObjectId("5e9297dbfe1792ce954f65f4")
        ]
}

3. db.collection.insert

Insert can insert a single document (object) / multiple documents (array of objects)

var data = [
    {"userid": 10, "operate": "login", "createtime": new date()},
    {"userid": 10, "operate": "click home", "createtime": new date()},
    {"userid": 10, "operate": "view list", "createtime": new date()}
]
db.logs.insert(data);

return
Single document

WriteResult({ "nInserted" : 1 })

Multiple documents

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})

four   If no insert exists

In daily operations, it is often necessary to write if it does not exist. In mongodb, an update mechanism (upsert) is used to write if it does not exist.

Db.logs.update ({"userid": 20}, {"userid": 20, "operate": "login", "createtime": new date()}, {upsert: true})

Methods that support the upsert parameter

  • db.collection.update()
  • db.collection.updateOne()
  • db.collection.updateMany()
  • db.collection.findAndModify()
  • db.collection.findOneAndUpdate()
  • db.collection.findOneAndReplace()

Read

db.logs.find({"UserId":20});
db.logs.findOne({"UserId":10});

 

Update

one    Update a single document

db.logs.updateOne({ "_id": ObjectId("5e92b6d4fe1792ce954f6613") }, { $set: { "CreateTime": new Date() } })

 

two   Update multiple documents

db.logs.updateMany({ "UserId": 10 }, { $set: { "CreateTime": new Date() } })

 

Delete

one   Delete a single document

db.logs.deleteOne({ "UserId": 20 })

 

two   Delete multiple documents

db.logs.deleteMany({ "UserId": 10})

Please indicate the source of forwarding: https://www.cnblogs.com/WilsonPan/p/12685393.html