In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the common commands in mongodb". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the common commands in mongodb.
I. commands commonly used in database
1. Help view command prompts
The copy code is as follows:
Help
Db.help ()
Db.yourColl.help ()
Db.youColl.find () .help ()
Rs.help ()
2. Switch / create database
The copy code is as follows:
Use yourDB; automatically creates the current database when you create a collection (table)
3. Query all databases
The copy code is as follows:
Show dbs
4. Delete the database currently in use
The copy code is as follows:
Db.dropDatabase ()
5. Clone the database from the specified host
The copy code is as follows:
Db.cloneDatabase ("127.0.0.1"); Clones the data from the database on the specified machine to the current database
6. Copy the specified database data from the specified machine to a database
The copy code is as follows:
Db.copyDatabase ("mydb", "temp", "127.0.0.1"); copy the local mydb data to the temp database
7. Repair the current database
The copy code is as follows:
Db.repairDatabase ()
8. View the database currently in use
The copy code is as follows:
Db.getName ()
The db; db and getName methods have the same effect, and both can query the currently used database.
9. Display the current db status
The copy code is as follows:
Db.stats ()
10. Current db version
The copy code is as follows:
Db.version ()
11. Check the address of the linked machine of the current db
The copy code is as follows:
Db.getMongo ()
2. Collection aggregation set
1. Create an aggregate collection (table)
The copy code is as follows:
Db.createCollection ("collName", {size: 20, capped: 5, max: 100}); / / if created successfully, {"ok": 1} will be displayed.
/ / determine whether the set is a fixed capacity db.collName.isCapped ()
2. Get the aggregate set with the specified name (table)
The copy code is as follows:
Db.getCollection ("account")
3. Get all the aggregate sets of the current db
The copy code is as follows:
Db.getCollectionNames ()
4. Display the status of all clustered indexes in the current db
The copy code is as follows:
Db.printCollectionStats ()
III. User relevance
1. Add a user
The copy code is as follows:
Db.addUser ("name")
Db.addUser ("userName", "pwd123", true); add user, set password, read-only or not
2. Database authentication and security mode
The copy code is as follows:
Db.auth ("userName", "123123")
3. Show all current users
The copy code is as follows:
Show users
4. Delete a user
The copy code is as follows:
Db.removeUser ("userName")
IV. Aggregate collection query
1. Query all records
The copy code is as follows:
Db.userInfo.find ()
Equivalent to: select* from userInfo
By default, 20 records are displayed per page, and when the display is not low, you can use the it iterative command to query the next page of data. Note: you cannot type the it command with ";"
But you can set the size of the data displayed on each page, using DBQuery.shellBatchSize= 50; that will show 50 records per page.
2. After the query is removed, the duplicate data of a column in the current aggregate collection is removed.
The copy code is as follows:
Db.userInfo.distinct ("name")
Will filter out the same data in name
Equivalent to: select distict name from userInfo
3. Query the records with age = 22
The copy code is as follows:
Db.userInfo.find ({"age": 22})
Equivalent to: select * from userInfo where age = 22
4. Query the records with age > 22
The copy code is as follows:
Db.userInfo.find ({age: {$gt: 22}})
Equivalent to: select * from userInfo where age > 22
5. Query age
< 22的记录 复制代码代码如下: db.userInfo.find({age: {$lt: 22}}); 相当于:select * from userInfo where age = 25的记录 复制代码代码如下: db.userInfo.find({age: {$gte: 25}}); 相当于:select * from userInfo where age >= 25
7. Query age = 23 and age 25
The copy code is as follows:
Db.userInfo.find ({age: {$gt: 25}}, {name: 1, age: 1})
Equivalent to: select name, age from userInfo where age > 25
13. Sort by age
The copy code is as follows:
Ascending order: db.userInfo.find () .sort ({age: 1})
Descending order: db.userInfo.find () .sort ({age:-1})
14. Query the data of name = zhangsan, age = 22
The copy code is as follows:
Db.userInfo.find ({name: 'zhangsan', age: 22})
Equivalent to: select * from userInfo where name = 'zhangsan' and age =' 22'
15. Query the first five pieces of data
The copy code is as follows:
Db.userInfo.find () .limit (5)
Equivalent to: selecttop 5 * from userInfo
16. Query the data after 10 items
The copy code is as follows:
Db.userInfo.find (). Skip (10)
Equivalent to: select * from userInfo where id not in (
Selecttop 10 * from userInfo
);
17. Query data between 5 and 10
The copy code is as follows:
Db.userInfo.find () .limit (10) .skip (5)
Can be used for paging, limit is the page of pageSize,skip * pageSize
18. Or and query
The copy code is as follows:
Db.userInfo.find ({$or: [{age: 22}, {age: 25}]})
Equivalent to: select * from userInfo where age = 22 or age = 25
19. Query the first piece of data
The copy code is as follows:
Db.userInfo.findOne ()
Equivalent to: selecttop 1 * from userInfo
Db.userInfo.find () .limit (1)
20. Query the number of records in a result set
The copy code is as follows:
Db.userInfo.find ({age: {$gte: 25}}) .count ()
Equivalent to: select count (*) from userInfo where age > = 20
21. Sort by a column
The copy code is as follows:
Db.userInfo.find ({sex: {$exists: true}}) .count ()
Equivalent to: select count (sex) from userInfo
5. Index
1. Create an index
The copy code is as follows:
Db.userInfo.ensureIndex ({name: 1})
Db.userInfo.ensureIndex ({name: 1, ts:-1})
2. Query all indexes of the current cluster
The copy code is as follows:
Db.userInfo.getIndexes ()
3. View the total index record size
The copy code is as follows:
Db.userInfo.totalIndexSize ()
4. Read all the index information of the current collection
The copy code is as follows:
Db.users.reIndex ()
5. Delete the specified index
The copy code is as follows:
Db.users.dropIndex ("name_1")
6. Delete all indexes
The copy code is as follows:
Db.users.dropIndexes ()
VI. Modify, add and delete collection data
1. Add
The copy code is as follows:
Db.users.save ({name: 'zhangsan', age: 25, sex: true})
The data column of the added data, which is not fixed, according to the added data.
2. Modification
The copy code is as follows:
Db.users.update ({age: 25}, {$set: {name: 'changeName'}}, false, true)
Equivalent to: update users set name = 'changeName' where age = 25
Db.users.update ({name: 'Lisi'}, {$inc: {age: 50}}, false, true)
Equivalent to: update users set age = age + 50 where name = 'Lisi'
Db.users.update ({name: 'Lisi'}, {$inc: {age: 50}, $set: {name:' hoho'}}, false, true)
Equivalent to: update users set age = age + 50, name = 'hoho' where name =' Lisi'
3. Delete
The copy code is as follows:
Db.users.remove ({age: 132})
4. Query modification and deletion
The copy code is as follows:
Db.users.findAndModify ({
Query: {age: {$gte: 25}}
Sort: {age:-1}
Update: {$set: {name: 'a2'}, $inc: {age: 2}}
Remove: true
});
Db.runCommand ({findandmodify: "users"
Query: {age: {$gte: 25}}
Sort: {age:-1}
Update: {$set: {name: 'a2'}, $inc: {age: 2}}
Remove: true
});
Either update or remove is a required parameter; the other parameters are optional.
Parameter detailed definition default value
Query query filter criteria {}
Sort if multiple documents meet the query filtering criteria, the first object will be selected in the arrangement specified by this parameter, and the object will be manipulated {}
If remove is true, the selected object will be deleted before being returned.
Update a modifier object
N/A
If new is true, the modified object is returned instead of the original object. This parameter is ignored during the delete operation. False
For fields, see Retrieving a Subset of Fields (1.5.0 +)
All fields
Upsert creates a new object if the query result is empty. Example (1.5.4+)
False
7. Statement block operation
1. Simple Hello World
The copy code is as follows:
Print ("Hello World!")
This way of writing calls the print function and writes "Hello World!" directly. The effect is the same.
2. Convert an object to json
The copy code is as follows:
Tojson (new Object ())
Tojson (new Object ('a'))
3. Add data in a loop
The copy code is as follows:
> for (var I = 0; I
< 30; i++) { ... db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2}); ... }; 这样就循环添加了30条数据,同样也可以省略括号的写法 复制代码代码如下: >For (var I = 0; I
< 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2}); 也是可以的,当你用db.users.find()查询的时候,显示多条数据而无法一页显示的情况下,可以用it查看下一页的信息; 4、find 游标查询 复制代码代码如下: >Var cursor = db.users.find ()
> while (cursor.hasNext ()) {
Printjson (cursor.next ())
}
In this way, all the users information can be queried, and it can be written the same way.
The copy code is as follows:
Var cursor = db.users.find ()
While (cursor.hasNext ()) {printjson (cursor.next);}
You can also omit the {} sign
5. ForEach iterative cycle
The copy code is as follows:
Db.users.find () forEach (printjson)
A function must be passed in forEach to process the data information of each iteration.
6. Treat find cursors as arrays
The copy code is as follows:
Var cursor = db.users.find ()
Cursor [4]
Get the data with a subscript index of 4
Now that you can treat it as an array, you can get its length: cursor.length (); or cursor.count ()
Then we can also display the data in a loop.
The copy code is as follows:
For (var I = 0, len = c.length (); I
< len; i++) printjson(c[i]); 7、将find游标转换成数组 复制代码代码如下: >Var arr = db.users.find () .toArray ()
> printjson (arr [2])
Convert it to an array with the toArray method
8. Customize our own query results
Show only age
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.