MongoDB basic operations summary

MongoDB syntax is summarized:
insert:
single insert
Syntax: insertOne ()
Example:

db.getCollection ( 'MY_TEST') insertOne. ({ " Date": "20190915", "author": "xmr", "goal": "mongo finishing grammar"})
1
results:

/ * *. 1 /
{
"Acknowledged": to true,
"insertedId": the ObjectId ( "5cdbdad464173957bc9e392a")
}
. 1
2
. 3
. 4
. 5
results demonstrate:

# Full table queries
db.getCollection ( 'MY_TEST') the Find ({}).

{
"_Id": ObjectId ( "5cdbdad464173957bc9e392a"),
"Date": "20190915",
"author": "XMR",
"goal": "syntax finishing mongo"
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
bulk insert:
syntax: insertMany ()
into the data set:

[{ "Name:": "xmr", "gender": "male", "address": "Earth", "age": "23", "income": "354", "hobby": "female" "occupation": "code livestock"},
{ "name:": "xmr", "gender": "male", "address": "Earth", "age": "23", "income": " 354 "," hobby ":" female "," occupation ":" code livestock "},
{" name: ":" xmr "," gender ":" male "," address ":" Earth "," age " : "35", "income": "233", "hobby": "female", "occupation": "code livestock", "education": "Bachelor"},
{ "name:": "zyd", " sex ":" female "," address ":"Mars", "age": "22", "income": "88888", "hobby": "eat", "occupation": "Students", "academic": "Master"},
{ "Name:" : "Wendy Zhang", "gender": "female", "Representative works": "Hello, the Chinese, the Tangshan earthquake", "variety": "longing for life in the third quarter", "advantage": "US" },
{ "animation name": "Naruto", "female": "on the Hinata", "age": "24"}
] '
. 1
2
. 3
. 4
. 5
. 6
. 7
example:Women "," Representative works ":" Hello, the Chinese, the Tangshan Earthquake "," Variety ":" longing for life in the third quarter "," advantage ":" US "}, {" anime name ":" Naruto Ninja "," female ":" on the Hinata "," age ":" 24 "} ] ' . 1 2 . 3 . 4 . 5 . 6 . 7 example:Women "," Representative works ":" Hello, the Chinese, the Tangshan Earthquake "," Variety ":" longing for life in the third quarter "," advantage ":" US "}, {" anime name ":" Naruto Ninja "," female ":" on the Hinata "," age ":" 24 "} ] ' . 1 2 . 3 . 4 . 5 . 6 . 7 example:

db.getCollection ( 'MY_TEST') insertMany ( [{ " Name:":. "xmr", " gender": "male", "address": "Earth", "age": "23", "income": "354", "hobby": "female", "occupation": "code livestock"},
{ "name:": "xmr", "gender": "male", "address": "Earth", "age ":" 23 "," income ":" 354 "," hobby ":" female "," occupation ":" code livestock "},
{" name: ":" xmr "," gender ":" male " "address": "Earth", "age": "35", "income": "233", "hobby": "female", "occupation": "code livestock", "education": "Bachelor"},
{ "name:": "zyd", "Sex ":" female "," address ":" Mars "," age ":" 22 "," income ":" 88888 "," hobby ":" eat "," occupation ":" Students "," academic " : "Master"},
{ "name:": "Wendy Zhang", "gender": "female", "Representative works": "hello, the Chinese, the Tangshan earthquake", "variety": "the first life longing three quarters "," advantage ":" US "},
{" anime name ":" Naruto "," female ":" Hinata Hyuga "," age ":" 24 "}
])
1
2
3
4
5
6
7
The result:Education ":" Master "}, {" Name: ":" Wendy Zhang "," gender ":" female "," Representative works ":" Hello, the Chinese, the Tangshan Earthquake "," Variety ":" yearning life in the third quarter "," advantage ":" US "}, {" anime name ":" Naruto "," female ":" Hinata Hyuga "," age ":" 24 "} ]) 1 2 3 . 4 . 5 . 6 . 7 run results:Education ":" Master "}, {" Name: ":" Wendy Zhang "," gender ":" female "," Representative works ":" Hello, the Chinese, the Tangshan Earthquake "," Variety ":" yearning life in the third quarter "," advantage ":" US "}, {" anime name ":" Naruto "," female ":" Hinata Hyuga "," age ":" 24 "} ]) 1 2 3 . 4 . 5 . 6 . 7 run results:} ]) . 1 2 . 3 . 4 . 5 . 6 . 7 Run Results:} ]) . 1 2 . 3 . 4 . 5 . 6 . 7 Run Results:

/ *. 1 * /
{
"Acknowledged": to true,
"insertedIds": [
the ObjectId ( "5cdbde9564173957bc9e392c"),
the ObjectId ( "5cdbde9564173957bc9e392d"),
the ObjectId ( "5cdbde9564173957bc9e392e"),
the ObjectId ( "5cdbde9564173957bc9e392f"),
the ObjectId ( "5cdbde9564173957bc9e3930" ),
the ObjectId ( "5cdbde9564173957bc9e3931")
]
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
NOTE: _id read as Object id, the time, machine code, and the process of incrementing counter configured pid

After inquiries and to delete all data just added several operating basis

Query
full table scan:
Syntax: find ({})
Example:

db.getCollection ( 'MY_TEST') ({}) Find.
1
Query fixed-value data:
Syntax: find ({ "field value 1": "value 1", "field value 2": "Value 2"})
Example 1 : the name is Wendy Zhang scanning table inside data

db.getCollection ( 'MY_TEST') find. ({ " Name:": "Wendy Zhang"})
1
Results 1:

/ * 1 * /
{
"_id": ObjectId ( "5cdbde9564173957bc9e3930"),
"Name:": "Wendy Zhang",
"gender": "female",
"Representative works": "Hello, the Chinese, the Tangshan Earthquake" ,
"variety": "longing for life in the third quarter",
"advantage": "US"
}
1
2
3
4
5
6
7
8
9
example 2: scanning table which name is xmr, undergraduate education data

db.getCollection ( 'MY_TEST') find. ({ " Name:": "xmr", "Education": "degree"})
1
Results:

/ * 1 * /
{
"_id": ObjectId ( "5cdbde9564173957bc9e392e"),
"Name:": "XMR",
"gender": "male",
"address": "Earth",
"age": "35"
"income": "233",
"hobby": "female",
"occupation": "code animal",
"education": "degree"
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
range query:
operator introduction:

greater than $ gt
$ GTE than or equal to
$ lt smaller than
$ LTE less
$ NE is not equal to
1
2
. 3
. 4
. 5
Example 1: a lookup table which is greater than equal to 35 years of age data

db.getCollection ( 'MY_TEST') find. ({ " Age": { "$ GTE": "35"}})
1
Results 1:

/ * 1 * /
{
"_id": ObjectId ( "5cdbde9564173957bc9e392e"),
"Name:": "XMR",
"gender": "male",
"address": "Earth",
"age": "35"
"income": "233",
"hobby": "female",
"occupation": "code animal",
"education": "degree"
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
example 2: a lookup table which were older than 23 and younger than 35, the result is equal.

db.getCollection('MY_TEST').find({"年龄" : {"$gt" : "23", "$lte" : "35"}})
1
结果2:

/ * 1 * /
{
"_id": ObjectId ( "5cdbde9564173957bc9e3931"),
"anime name": "Naruto",
"female": "Hinata Hyuga",
"age": "24"
}

/ * 2 * /
{
"_id": ObjectId ( "5cdbde9564173957bc9e392e"),
"Name:": "XMR",
"gender": "male",
"address": "Earth",
"age": "35"
"income": "233",
"hobby": "female",
"occupation": "code animal",
"education": "degree"
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
to continue adding the above conditions on the basis of: (example 2 and the results meet Naruto called animation)
. 1
db.getCollection ( 'MY_TEST').find ({ "Age": { "$ gt": "23", "$ lte": "35"}, " animation name": "Naruto"})
1
Results:

/ * *. 1 /
{
"the _id": the ObjectId ( "5cdbde9564173957bc9e3931"),
"animation name": "Naruto",
"female": "on the Hinata",
"age": "24"
}
. 1
2
. 3
. 4
. 5
. 6
. 7
return or reject the specified fields:
a field representative of the return
0 for this field culling
example 1: Age and returns only the name field

db.getCollection ( 'MY_TEST') find. ({}, { " Age": 1, "Name": 1})
1
Results 1:

Example 2: In addition to age and name fields, and the rest show all:

db.getCollection ( 'MY_TEST') find. ({}, { " Age": 0, "Name:": 0})
1
Results:


The number of query results or limit the number of
query results Syntax number: count ()
limit the number of returning entries syntax: limit (item number)
query result number of instances:

db.getCollection ( 'MY_TEST') Find ({}) COUNT ();..
. 1
Results: 8
limiting examples of records returned: Returns two results:

.. db.getCollection ( 'MY_TEST') Find ({}) limit (2)
. 1
Results:

/ * 1 * /
{
"_id": ObjectId ( "5cdbdad464173957bc9e392a"),
"Date": "20190915",
"author": "XMR",
"goal": "Finishing mongo grammar"
}

/ * 2 * /
{
" the _id ": the ObjectId (" 5cdbdbac64173957bc9e392b "),
" date ":" 20190915 ",
" OF ":" XMR ",
" target ":" finishing mongo syntax "
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
Sort
syntax: sort ( "field name": 1 or -1) where: 1 represents positive sequence, reverse -1 represents
example a: the older than younger than 23 and 35 equal results in reverse order according to age

db.getCollection ( 'MY_TEST') find ( { " Age": { "$ gt": "23", "$ lte": "35"}}).. sort ({ " Age": -1})
1
returned the following results:


Modify Operation:
Syntax: updateOne () to modify a single data
updateMany () to modify a plurality of data
, for example: the table inside all younger than 23 data name changed to Hinata Hyuga

db.getCollection ( 'MY_TEST') updateMany. ({ " Age": { "$ lte": "23"}}, { "$ set": { " Name:": "Hinata Hyuga"}})
1
execution result:

/ * 1 * /
{
"Acknowledged": to true,
"matchedCount": 3.0,
"ModifiedCount": 3.0
}
`` `
From the above results, there are three records are changed, Inquiry Form view to modify the results:

` `` the Java
db. getCollection ( 'MY_TEST') find. ({ " Age": { "$ LTE": "23"}})
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
can be seen: the age of 23 or less are all the result of the name modify become Hinata Hyuga


Delete operation, use caution!
Syntax: deleteOne () deletes a
deleteMany () delete several
examples of 1: Delete table inside the first record aged 35

db.getCollection ( 'MY_TEST') deleteOne: . ({ " age" of "35"})
1
Run after deletion:

. db.getCollection ( 'MY_TEST') Find ({})
. 1
can be seen from the results, the age of the original 35 is a record deleted:


Example 2: Delete table inside of all records for the xmr

db.getCollection ( 'MY_TEST') deleteMany. ({ " OF": "XMR"})
. 1
The results: it was found that two records are deleted

/ * *. 1 /
{
"Acknowledged": to true,
"DeletedCount": 2.0
}
. 1
2
. 3
. 4
. 5
query the database:

. db.getCollection ( 'MY_TEST') the Find ()
1
can be seen: All of the data xmr all been deleted


Data deduplication
Syntax: DISTINCT ()
Example 1: age fields deduplication

. db.getCollection ( 'MY_TEST') distinct ( " Age")
1
Result 1:

/ * 1 * /
[
"23",
"24",
"22"
]
1
2
. 3
. 4
. 5
. 6
Example 2: age 23 is greater than or equal to the results of weight

db.getCollection ( 'MY_TEST') distinct. ( " Age", { "Age": { "$ GTE": "23 is"}})
. 1
returns the result:

/ * 1 * /
[
"23",
"24"
]
1
2
. 3
. 4
. 5
Note: distinct (http://www.my516.com) after having allowed the weight to other fields, so its use has a great limitations!
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10987490.html