Compared to relational databases, Array [1,2,3,4,5] and Object {' name ': ' Dragonfire '} are MongoDB's more special types.
Where is the special? What needs to be noticed in their operations?
Let's start with a piece of data that contains the Array and Object types
Db. Oldboy.insert ({"Name": "Lu Fei Learning City-Knight Plan", "Price": [19800,19500,19000,18800], "other": {"Start": "August 1, 2018", "Start_time": "08:30", "Count": 150}})
{ "_id": ObjectId ("5b17d01a49bf137b585df891"), "name":"Luffy City-Knight plan", " Price" : [ 19800, 19500, 19000, 18800 ], " Other" : { "Start":"August 1, 2018", "start_time":"08:30", "Count": 150 }}
Okay, this data is done.
For this data we do a series of operations, and explain how to use
An. Array
1. Change price from 19000 to 19300
Db. Oldboy.update ({"Name": "Luke City-Knight Plan"},{$set: {"price.2": 19300}})
We changed the value by referencing the subscript, and the "price.2" refers to the 3rd element in the Array.
2. Mixed usage
If the price.1 is less than 19800, add 200.
Db. Oldboy.update ({"Name": "Luke City-Knight Plan", "price.1": {$lt: 19800}},{$inc: {"price.1": 200}})
Auto-fill 200 with price less than 19500
Db. Oldboy.updatemany ({"Name": "Road flying Learning City-Knight Plan", "price": {$lt: 19500}},{$inc: {"price.$": 200}})
Careful classmate has found, only changed the first one, yes $ this only stores a subscript, batch change, Hee hee as of January 1, 2017, MongoDB does not have this function
You just take this array out, in the program inside change, the original side does not move to put back not to be done?
Two. Object
It's like a dictionary, but it's more like an object in JavaScript.
1. Change the count of other to 199
Db. Oldboy.update ({"Name": "Luke City-Knight Plan"},{$set: {"Other.count": 199}})
By the way, it is possible to change the value with a bit of key in this object, and to be aware that the $set that we use is modified, which means that if there is no "other.count" field, he will automatically create
This usage is here, let's play a deeper
2. Mixed usage
If count is less than 200 then add 10
Three. Use of Array + Object
First, you must first create a Document
{ "_id": ObjectId ("5b17de9d44280738145722b9"), "name":"Luffy City-Knight plan", " Price" : [ { "Start":"August 1, 2018", "start_time":"08:30", "Count": 150 }, { "Start":"August 2, 2018", "start_time":"09:30", "Count": 160 }, { "Start":"August 3, 2018", "start_time":"10:30am", "Count": 170 }, { "Start":"August 4, 2018", "start_time":" the", "Count": 180 } ]}
1. Add 15 to field with count greater than 175
Mashup Complete:
Db. Oldboy.update ({"Price.count": {$gt: 175}},{$inc: {"Price.$.count": 15}})
2. Change the start of count greater than 180 to "August 10, 2018"
The special operation of the Array Object of MongoDB Three