Basic arrangement of mongodb -- General operation [2]

preface

Simply sort out the general operations.

text

Although generally speaking, writing code depends on ideas, how can you know whether mongodb meets your needs if you don't know what common operations mongodb has? For example, if you don't have aggregation functions and you have to write aggregation yourself, then the workload is still quite large and does not meet the requirements of software development.

Insert a piece of data: dB fruit. insertOne({'name':'apple'})

Insert multiple: dB fruit. insertMany([{'name':'apple1'},{'name':'apple2'}])

Then find has many functions, such as:

name must be equal to apple.

Then we naturally think about whether find has and and or operations.

db.fruit.find({"name":"apple","number":10});

This multiple condition is and.

Of course, it can also be written as follows: dB fruit. find({$and:[{"name":"apple"},{"number":10}]});

The lookup method also supports regular expressions.

Similarly, how to write the operator?

(>) greater than - $gt
(<) less than - $lt
(>=) Greater than or equal to - $gte
(<= ) Less than or equal to - $lte

For example, the quantity should be greater than 1:

It also supports many, such as:

 $ne Not equal to
 $exits Is it empty
 $in Query interval {a:{$in:[1,2,3,4]}}
 $nin Not in query interval

mongo has sub documents. How to query the sub documents, for example:

db.fruit.insertOne({"name":"pear","number":100,"from":{
    "country":"china",
    "province":"jiangxi"
}})

There is such a record in it.

When querying, it is written as follows:

So can I write this:

It seems that it doesn't work, but there is no error, so there is a question. What does this do?

We know that: is equal, so this means that from should be equal to {"country":"china"}

Try it, ha:

db.fruit.insertOne({"name":"pineapple","number":100,"from":{
    "country":"china"
}})

Then check:

There is another question, because we support arrays. How to query the things in the array, for example:

db.fruit.insertMany([{"name":"colorApple","color":["red", "yellow"]
},{"name":"colorBanana","color":["red", "green"]}])

Just use:.

You can also use $in: dB fruit. find({"color":{$in:["red"]}})

If not, you can: dB fruit. find({"color":{$nin:["red"]}})

This is still very convenient.

Another thing to note about mongodb array query:

{
    "_id" : ObjectId("5e6b4ef546b5f44e5c5b276d"),
    "name" : "Zhao Xiaoming",
    "used_name" : [ 
        "Zhao Ming", 
        "Zhao Xiaopeng"
    ],
    "age" : 16,
    "gender" : 0,
    "relatives" : [ 
        {
            "name" : "Zhao Gang",
            "relationship" : 0
        }, 
        {
            "name" : "Xiuying",
            "relationship" : 1
        }
    ]
}

For example, this record:

db.people.find({
    "relatives.name": "Zhao Gang", "relatives.relationship": 1
})

I found it. Requirements are related Name if Zhao Gang, relations The relationship must be 1, which is the relationship of and.

In the and of the mongo sub document, it needs to be written as follows:

db.people.find({
    "relatives":{ $elemMatch:{"name":"Zhao Gang", "relationship": 1}}
})

As can be seen from the above, we return the whole record. What if we only want a certain field?

db.people.find({
    "relatives":{ $elemMatch:{"name":"Zhao Gang"}}
},{name:1})

That's just name.

Do you want all the other fields?

db.people.find({
    "relatives":{ $elemMatch:{"name":"Zhao Gang"}}
},{name:0})

There are all but name here.

Then, with the addition and query, how to delete?

First, there is a very dangerous operation: dB fruit. Remove ({}). If an empty document is passed in, all will be deleted.

db.fruit.remove({"name":"apple"})

Tell us to delete one.

db.fruit.updateOne({"name":"apple1"},{$set:{from:{country:"china"}}})

Change this to china.

There are updateOne and updateMany. We know from the literal meaning that one is to update one and the other is to update all.

If there is set, there is unset:

db.fruit.updateOne({"name":"colorApple"},{$unset:{color:"red"}})

If there is red in color, delete it.

When mongo updates, it still gives special preferential treatment to arrays:

$push Add an object to the bottom of the array
$pushAll Add multiple objects to the bottom of the data
$pop Deletes an object from the bottom of the array
$pull If the specified value matches, the corresponding object is deleted from the array
$pullAll If any value matches, the corresponding object is deleted from the data
$addToset If it does not exist, add a value to the array

What if you delete the entire collection?

You can use:

db.fruit.drop()

This drop will delete all indexes, not just empty the database.

junction

Next section aggregate query

Turn https://www.cnblogs.com/aoximin/p/15780677.html

Keywords: MongoDB

Added by Worqy on Mon, 17 Jan 2022 15:00:38 +0200