search
HomeDatabaseMysql TutorialMongo客户端常用命令

Mongo客户端常用命令

Jun 07, 2016 pm 02:53 PM
mongoOrderclientCommonly used

Mongo客户端常用命令 一、数据库相关命令 1 show dbs // 列出所有数据库 2 use memo // 使用数据库memo。即使这个数据库不存在也可以执行,但该数据库不会立刻被新建,要等到执行了insert之类的操作时,才会建立这个数据库 www.2cto.com 3 show collections


Mongo客户端常用命令

 

一、数据库相关命令   

1  show dbs // 列出所有数据库     

2  use memo // 使用数据库memo。即使这个数据库不存在也可以执行,但该数据库不会立刻被新建,要等到执行了insert之类的操作时,才会建立这个数据库   

  www.2cto.com    

3   show collections // 列出当前数据库的collections    

4   查看各collection的状态  db.printCollectionStats()    

5   db // 显示当前数据库     

6   show users // 列出用户    

7   db.system.users.find()  // 列出用户    
 

8   db.removeUser('user1') //删除用户   

9   db.c1.drop()//删除集合c1   

10  db.dropDatabase()//删除当前的数据库   二、安全与认证   

1  use shine // 如果要root权限,就用admin库     

  www.2cto.com  

2   db.addUser("username", "password") // 普通权限,可读写     

3  db.addUser("username", "password", true)  // 只可读,不可写     
 

4   db.system.users.remove({user: username}) // 删除用户     1   db.c1.save({name:"zhangsan",age:18}) //没有写主键,系统会自动生成一个主键,主键名为_id,    2 .每个MongoDB的document都有一个_id字段作为它的第一个属性,这个值通常是一个BSON对象id,因此,这个id对于集合中的每个成员 都是唯一的,如果用户插入一个document没有提供一个id,数据库将自动生成一个id,并存储在_id字段。   
 

3. db.c1.save({_id:1,name:"lisi",age:22})//自己填写id主键值,id主键值可以是字符串类型,也可以是数字类型   

 

4. db.c1.save({"name" : "MongoDB","type" : "database","count" : 1,"info" : {x : 203,y : 102}})       四、创建索引:   

1   coll.ensureIndex({productid:1}) // 在productid上建立普通索引     

2   coll.ensureIndex({district:1, plate:1}) // 多字段索引     

3   coll.ensureIndex({productid:1}, {unique:true}) // 唯一索引     

4   coll.ensureIndex({productid:1}, {unique:true, dropDups:true|) // 建索引时,如果遇到索引字段值已经出现过的情况,则删除重复记录     

 

5   coll.getIndexes() // 查看索引     

6   coll.dropIndex({productid:1}) // 删除单个索引     1.  db.coll.find() // select * from coll    

2.  db.coll.find().limit(10) // select * from coll limit 10    

3.  db.coll.find().sort({x:1}) // select * from coll order by x asc    

4.  db.coll.find().sort({x:-1}) // select * from coll order by x desc    

5.  db.coll.find().sort({x:1}).skip(5).limit(10) // select * from coll order by x asc limit 5, 10    

 

6.  db.coll.find({x:10}) // select * from coll where x = 10    

7.  db.coll.find({x: {$lt:10}}) // select * from coll where x

8.  db.coll.find({}, {y:true}) // select y from coll    

9.  通过游标访问数据  

.> var cursor = db.collect1.find();  

> while(cursor.hasNext()) printjson(cursor.next())  

当数据超过20行时候,使用it命令查看更多数据      通过forEach  

10> db.collect1.find().forEach(printjson)  

 

11. 在mongo shell中,你可以将游标认为是数组  

12> var cursor =db.collect1.find();  

13.> printjson(cursor[4])  

14{ "_id" : ObjectId("4c691e72ed2a47b462dfa806"), "x" : 4, "y" : 3 }  

使用这种方式存取需要注意的是,在cursor[4]之前的所有数据都会同时被加载到内存,对于很大的结果集,这样操作是不恰当的,会导致内存溢出,当查询巨大数据量大时候,游标应当作为迭代器使用。

 www.2cto.com  

SQL语句不能做到的,mongo也可以做到  

1.  coll.find({"address.city":"gz"}) // 搜索嵌套文档address中city值为gz的记录    

2.  coll.find({likes:"math"}) // 搜索数组    

3.  coll.ensureIndex({"address.city":1}) // 在嵌套文档的字段上建   1.  db.user.update({uid:1},{$set:{age:26}}) //update user set age=26 where uid=1  
 

2.  db.user.update({uid:1},{$inc:{age:1}}) //update user set age=age+1 where uid=1   1.db.user.delete({uid:1})//delete user where uid=1 

2.db.Position.remove({"id":10}) //delete * from Position where id=10 1.  // json或csv格式,每次一个collection     

 

mongoexport -d producttrade -c basic -o /home/data/mongo_backup/producttrade_100504.json    

145.3.  mongoimport -d producttrade -c basic --drop /home/data/mongo_backup/producttrade_100504.json      // 二进制数据格式,常用于备份、还原     

2  mongodump -d shine -o /home/data/mongo_backup     

3  mongorestore -d shine --drop /home/data/mongo_backup/shine   

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.