MongoDB study notes-continuous updates

Basic Commands

In this note
collection = table
document = row
object = key
attribute = value

(1) Insert document

(1) db.collection.insert ({}) #insert
one or more documents

> db.test.insert({
 		_id:1,name:'zbj',
 		age:18,
 		addres:'zhu'
 })
WriteResult({ "nInserted" : 1 })  ##插入一个或多个文档

(2) db.collection.insertOne ({}) #When
filling in multiple documents at the same time, only the first document will be inserted

(3) db.collection.insertMany ([{}, {}])
#Only allow multiple documents to be inserted

> db.test.insertMany([{
	   "_id" : 2,
	   "name" : "zbj",
	   "age" : 18,
	   "addres" : "zhu" },
	   { "_id" : 4,
	   "name" : "zbj",
 	   "age" : 18,
 	   "addres" : "zhu"}
 ])

(4) Define variables

> document=({title: 'MongoDB', 
    by: 'cc',
    url: 'www',
    tags: ['mongodb', 'database', 'NoSQL'],
});

(5) Then perform the insert operation

db.test.insert(document)

(Two) query documents

(1) db.collection.find () #Find
all documents by default
Note: find () returns an array, that is, you can search by index

db.test.find()[0]

db.collection.find ({Condition 1, Condition 2})
#Condition query (and)

db.test.find({_id:'1'})

(2) db.collection.findOne () #Query
the first document
that meets the conditions Note: The document object returned


(3) db.collection.find ({}). The function
skip () skips the specified amount of data
lilmit () to limit the number of items displayed on each page
Insert picture description here
.

db.collection.find( { } ).count()   #文档数
db.collection.find( { } ).length()   #同上
db.qq1.find().skip(10).limit(10)  #跳过前十条查询接下来的十条

Conditional query

For example, find documents with num greater than 200 and less than 300

 db.test.find({num:{$gt:200,$lt:300}});

Such as

(3) Modify the document

(1) db.collection.update ({query object and attribute}, {replacement object and attribute})
#The latter document will replace all the documents queried by the former, and
only useful for the first document queried

db.test.update({
    _id:'1'},
    {name:'5',
    home:'2'
    })

(2) db.collection.save ({replace content}) #Replace
all content in the collection

(3) db.test.replaceOne ({name: "cgp"}, {name: "cgp240"})
db.test.replaceMany ({name: "cgp"}, {name: "cgp240"}) to
replace the specified content


To modify specified objects or attributes, use the "modify operator":

(1) $ set
Note that if the specified object does not exist, a new object will be added. Update () only modifies the first matching document by default, and modifying multiple ones requires updateMany ()

db.test.update({
    查询对象},
    {$set{  
    指定对象替换
 	}})   #不能用于添加属性

#Delete the specified object $ unset

db.test.update({
    查询对象},
    {$unset{  
    指定对象
 	}})

(2) $ push: you can add
$ addToset: do not add existing attributes
to add attributes to the array

>{ "_id" : ObjectId("5e8d7a48315dd25b1a6d92c5"),
 "name" :{ "test" : [ 1, 2, 3, 4] },
 "home" : "湖南" }

>db.test.update({
	home:"湖南"},
	{$push:{"name.test":5
	}})
(3) The document attribute of Mongodb can be a document

When the attribute of a document is a document, the document is called an embedded document

 db.test.update({
 	name:"240"},
 	{$set:{name:{"ming":"陈哥"}
 	}});

Inline document query method: db.xxx.find ({"first object. Second object": "attribute"})

db.test.find({"name.ming":"陈哥"})

(4) Delete the document

db.collection.remove ({}) #Delete
all documents that meet the conditions by default


db.collection.remove ({}, true) #Delete
the first document that meets the conditions


db.collection.deleteOne ({}) #Delete
the first document that meets the conditions


db.collection.deleteMany ({}) #Delete
all documents that meet the conditions


Empty collection

db.collection.drop()

Released eight original articles · won praise 5 · Views 2395

Guess you like

Origin blog.csdn.net/qq_46341303/article/details/105369381