Add, delete, modify, and check mongoDB command window operations, mongoDB database foundation

1. Start the MongoDB client to enter the shell and
after the server configuration is successful, we should not close the server DOS window, otherwise it will not be able to connect!
We open another command window, go to F:\MongoDB:\bin, enter the mongo.exe command to connect to
the database 2. • Query all databases show dbs or show databases
When we want to create a new database that does not exist, for example we If you want to create a new database testTwo, you can directly enter the command use testTwo. If testTwo exists, switch back to testTwo. If it does not exist, it will create a new database.
3. Under mongodb, the corresponding sql table is called collection. There is no concept of table in mongo, only the concept of collection. Similar to database, collection does not need to be newly created separately. When inserting records, if there is a collection, insert data into the collection. In the collection, if the collection does not exist, create a new collection.
- db.user.save({"name":"wpz","password":"123"}); (user refers to the collection name)
4. Conditional data query is to add filter conditions to the query, mongodb's The precise filter condition is to formulate the json data in the query data. For example db.user.find({"age";"20"}) is equivalent to select * from user where age = '20' in sql
•db.user.find({"age";"20"}) This Combined with the above ordinary query, you can query most of the desired data, but sometimes we need to specify the fields of the query:
5. mongodb also provides the findOne() method in order to reduce the memory overhead of the cursor. Of course, within the method You can add filter conditions
. db.user.findOne()
.db.user.findOne({"name":"wpz"})
Both of these are legal.
6. mongodb also provides limit to limit the number of entries
. db.user.find().limit(2)
. db.user.find({"name":"wpz"}).limit(2)
7. Conditional query mongodb supports < <= > >= four operators query
db.user.find({“age”:{$gt:30}}) age is greater than 30
db.user.find({“age”:{$lt: 30}}) age is less than 30
db.user.find({"age":{$gte:30}}) age is greater than or equal to 30
db.user.find({"age":{$lte:30}}) age is less than or equal to more than 30
conditions query
db.user.find({“age”:{,gt:10,lte:30}})
6, match all
$all This operator is similar to the in operator in sql, but The difference is that in only needs to satisfy one value, but all needs to satisfy all values.
db.user.find({“age”:{$all:[6,8]}});
7. Query whether a field exists: $exists
db.user.find({“password”:{$exists: true}}); password exists record
db.user.find({“password”:{$exists:false}}); Record
8, null is worth dealing with without password It
    is strange to deal with null, because the data collection in mongodb cannot specify a specific format, there is no sql The concept of the field, that is to say, some fields in the same collection exist in one piece of data, but do not exist in another piece of data, so, to find out whether the field is empty, you must first determine whether the field exists. Just do it.
    db.user.find({age:{"in":[null],"exists":true}});
9, the modulo operation $mod
    queries all the data whose age is 0 after modulo 10, that is, the query age is Fields that are multiples of 10:
    db.user.find({age:{$mod:[10,0]}});
10, not equal to $ne –> (not equals) Query all data     db
    whose age is not equal to 10 .
user.find({age:{$ne:10}});
11, contains $in
    to query all data whose age is equal to 10 or 20
    db.user.find({age:{$in:[10,20]}} );
12,
    Query all data whose age is not equal to 10 or 20 without $nin
    db.user.find({age:{$nin:[10,20]}});
13,Number of array elements $size
    Query data whose age data element number is 3
    db.user.find({age:{$size:3}});
14, the regular expression matches
     the data whose query name does not start with wpz
     db.user.find({“ name”:{$not:/^wpz.*/}});
15,count the number of queries
- db.user.find().count();
16,skip Set the starting point of the query data. The
query starts from the third data After the five data
- db.user.find().skip(3).limit(5);
17 Sort sort
- db.user.find().sort({age:1}); In ascending order of age
- db. user.find().sort({age:-1}); Descending by age
MongoDB also supports stored procedure queries.
18. Data modification and update
    (1). The modification of mongodb is a more annoying one, and $set is used:
    (2). For example, in mongodb, the name is wpz, and the modification is wpz_new
     db.user.update({“ name":"wpz"},{$set:"name":"wpz_new"});
19. Data deletion
    1. The deletion of mongodb is relatively simple, the format is as follows:
    db.user.remove({“name”:”wpz”});



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529364&siteId=291194637