Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB grammar practice

Shulou Source: shulou.com Published: 2022-06-01 22:17:39 09月16日 Update

# simple operation procedure

Basic query:

Construct query data:

Db.test.insert ({name: "stephen", age:35,genda: "male", email: "stephen@hotmail.com"})

Db.test.insert ({name: "Stephen", age:35,genda: "male", email: "stephen@hotmail.com"})

Db.test.insert ({name: "stephen1", age:35,genda: "male", email: "stephen@hotmail.com"})

Db.test.insert ({name: "stephen", age:36,genda: "male", email: "stephen@hotmail.com"})

Db.test.insert ({name: "stephen", age:37,genda: "male", email: "stephen@hotmail.com"})

-- Multi-conditional query, which is equivalent to where name = "stephen" and age = 35 of the SQL statement

Db.test.find ({name: "stephen", age:35})

{"_ id": ObjectId ("59f1ae4388f3edba94091894"), "name": "stephen", "age": 35, "genda": "male", "email": "stephen@hotmail.com"}

-- returns the specified document key-value pair, and only name and age key-value pairs

Db.test.find ({}, {name:1,age:1})

{"_ id": ObjectId ("59f1ae4388f3edba94091894"), "name": "stephen", "age": 35}

{"_ id": ObjectId ("59f1ae8588f3edba94091895"), "name": "stephen", "age": 36}

{"_ id": ObjectId ("59f1ae8a88f3edba94091896"), "name": "stephen", "age": 37}

2. Query conditions

MongoDB provides a set of comparison operators: $lt/$lte/$gt/$gte/$ne, which in turn is equivalent to = /! =

-- returns the result that meets the condition age > = 18 & & age = 18 & & age n. Instead of using $size directly, you can only add an additional key to represent the element data in the data. When manipulating the elements in the data, you need to update the value of the size key at the same time.

-- construct data for later experiments

Db.wqq.update ({}, {"$set": {"size": 3}}, false,true)

WriteResult ({"nMatched": 3, "nUpserted": 0, "nModified": 3})

> db.wqq.find ()

{"_ id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["aa", "bb", "cc"], "size": 3}

{"_ id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["aa", "ee", "cc"], "size": 3}

{"_ id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["ff", "ee", "cc"], "size": 3}

Each time a new element is added, atomic self-increment size is required.

Db.wqq.update ({}, {$push: {name:123}, $inc: {size:1}}, 0prime1)

WriteResult ({"nMatched": 3, "nUpserted": 0, "nModified": 3})

> db.wqq.find ()

{"_ id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["aa", "bb", "cc", 123], "size": 4}

{"_ id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["aa", "ee", "cc", 123], "size": 4}

{"_ id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["ff", "ee", "cc", 123], "size": 4}

Returns some of the data in the array through $slice. "$slice": 2 represents the first two elements in the array

Db.wqq.find ({}, {name: {$slice:2}, size:0}) / / if negative, it is the last two elements

{"_ id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["aa", "bb"]}

{"_ id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["aa", "ee"]}

{"_ id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["ff", "ee"]}

-- $slice: [2jue 1], which means to take one element from the second 2 element. If the number of elements after the second element is greater than 2, then all the subsequent data will be taken.

Db.wqq.find ({}, {name: {$slice: [2jue 1]}})

Db.wqq.find ({}, {name: {$slice: [2jue 1]}, size:0})

{"_ id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["cc"]}

{"_ id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["cc"]}

{"_ id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["cc"]}

6. Embedded document query

-- when the embedded document is an array, the $elemMatch operator is required to help locate the match of an element, otherwise the embedded file will match it all.

-- that is, all the elements need to be listed as query conditions when searching.

Db.aa.insert ({comments: [{author: "sharesoe", score:3}, {author: "mary", score:6}]})

WriteResult ({"nInserted": 1})

> db.aa.find ()

{"_ id": ObjectId ("59f1cc8e88f3edba9409189f"), "comments": [{"author": "sharesoe", "score": 3}, {"author": "mary", "score": 6}]}

--

Db.aa.find ({comments: {$elemMatch: {author: "sharesoe", score: {$gte:3})

{"_ id": ObjectId ("59f1cc8e88f3edba9409189f"), "comments": [{"author": "sharesoe", "score": 3}, {"author": "mary", "score": 6}]}

Tags: Element data array query document condition retrieval example faceted operator subscript at the same time embedded case quantity regular syntax precision consistency two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Redmi NVidia OPPO Reno