Add delete change and search orders
- 2020-06-03 08:41:45
- OfStack
1 List and select
1.1 List all databases
> show dbs
local 0.000GB
myblog 0.000GB
1.2 Use a database
> use myblog
switched to db myblog
1.3 List all collections
> show collections
articles
replicationColletion
sessions
users
wangduanduan
2 Insert data insert(value)
// Inserts data into an existing collection
> db.users.insert({name:'hh',age:23})
Inserted 1 record(s) in 43ms
// Inserts data into a collection that does not exist , The collection is automatically created and inserted when it does not exist
> db.students.insert({name:'hh',age:23})
Inserted 1 record(s) in 72ms
Three query find (option)
3.1 Query all documents in the collection
> db.users.find()
/* 1 */
{
"_id" : ObjectId("583e908453be942d0c5419dc"),
"login_name" : "wangduanduan",
"password" : "wrong age"
}
/* 2 */
{
"_id" : ObjectId("583ed2a5cc9a937db049616d"),
"login_name" : "hh",
"password" : "sdfsdf"
}
/* 3 */
{
"_id" : ObjectId("583fb2e9b12f8b7a7aa37572"),
"name" : "wangduanduan",
"age" : 34.0
}
/* 4 */
{
"_id" : ObjectId("583fb707b12f8b7a7aa37573"),
"name" : "hh",
"age" : 23.0
}
3.2 Query documents by conditions
> db.users.find({name:'wangduanduan'})
/* 1 */
{
"_id" : ObjectId("583fb2e9b12f8b7a7aa37572"),
"name" : "wangduanduan",
"age" : 34.0
}
Pay attention to
// That's wrong. I can't find the results
> db.users.find({_id:'583fb2e9b12f8b7a7aa37572'})
Fetched 0 record(s) in 1ms
// This is correct
> db.users.find({_id:ObjectId('583fb2e9b12f8b7a7aa37572')})
/* 1 */
{
"_id" : ObjectId("583fb2e9b12f8b7a7aa37572"),
"name" : "wangduanduan",
"age" : 34.0
}
3.3 Number of documents in the query collection
> db.users.count()
4
3.4 Comparison operators
$gt: greater than
$gte: greater than or equal to
$lt: less than
$lte: less than or equal to
$ne: is not equal to
// The query user is older than 30 The age of, And so on
> db.user.find({age:{$gt:30}})
/* 1 */
{
"_id" : ObjectId("583fb2e9b12f8b7a7aa37572"),
"name" : "wangduanduan",
"age" : 34.0
}
3.5 Logical operators
3.5.1 track of and
> use myblog
switched to db myblog
0
3.5.2 $in or
> use myblog
switched to db myblog
1
3.5.3 $nin non
> use myblog
switched to db myblog
2
3.6 Regular matching
// The query name is contained in duan The user
> db.users.find({name:/duan/})
/* 1 */
{
"_id" : ObjectId("583fb2e9b12f8b7a7aa37572"),
"name" : "wangduanduan",
"age" : 34.0
}
/* 2 */
{
"_id" : ObjectId("583fc919b12f8b7a7aa37575"),
"name" : "wangduanduan",
"age" : 45.0
}
3.7 big recruit $where
> use myblog
switched to db myblog
4
4 update update ();
4.1 Overall Update
> db.users.update({login_name:'wangduanduan'},{name:'heihei',age:34})
Updated 1 existing record(s) in 116ms
4.2 $set partial update
// Just set the user age to 101
> db.users.update({name:'wangduanduan'},{$set:{age:101}})
4.3 $inc
// Increase the user's age by 1 year, if the document does not have the age field
> db.users.update({name:'wangduanduan'},{$inc:{age:1}})
4.3 upsert operation
// If the document cannot be found, add the document
> db.users.update({name:'nobody'},{$inc:{age:1}},true)
Updated 1 new record(s) in 3ms
/* 6 */
{
"_id" : ObjectId("583fd20f2cfa6a4817c4171c"),
"name" : "nobody",
"age" : 1.0
}
4.4 Batch update
When the fourth parameter of upadate is set to true, it will be updated in batches
> db.users.update({name:'wangduanduan'},{$set:{age:1891}},false,true)
5 to delete
// Delete some documents
db.person.remove({"name":"joe"})
// Delete the entire collection
db.person.remove()