Mongo客户端常用命令
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![VMware Horizon Client cannot be opened [Fix]](https://img.php.cn/upload/article/000/887/227/170835607042441.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
VMware Horizon Client helps you access virtual desktops conveniently. However, sometimes the virtual desktop infrastructure may experience startup issues. This article discusses the solutions you can take when the VMware Horizon client fails to start successfully. Why won't my VMware Horizon client open? When configuring VDI, if the VMWareHorizon client is not open, an error may occur. Please confirm that your IT administrator has provided the correct URL and credentials. If everything is fine, follow the solutions mentioned in this guide to resolve the issue. Fix VMWareHorizon Client Not Opening If VMW is not opening on your Windows computer
![VMware Horizon client freezes or stalls while connecting [Fix]](https://img.php.cn/upload/article/000/887/227/170942987315391.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
When connecting to a VDI using the VMWareHorizon client, we may encounter situations where the application freezes during authentication or the connection blocks. This article will explore this issue and provide ways to resolve this situation. When the VMWareHorizon client experiences freezing or connection issues, there are a few things you can do to resolve the issue. Fix VMWareHorizon client freezes or gets stuck while connecting If VMWareHorizon client freezes or fails to connect on Windows 11/10, do the below mentioned solutions: Check network connection Restart Horizon client Check Horizon server status Clear client cache Fix Ho

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

MQTT (MessageQueuingTelemetryTransport) is a lightweight message transmission protocol commonly used for communication between IoT devices. PHP is a commonly used server-side programming language that can be used to develop MQTT clients. This article will introduce how to use PHP to develop an MQTT client and include the following content: Basic concepts of the MQTT protocol Selection and usage examples of the PHPMQTT client library: Using the PHPMQTT client to publish and

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features
