MongoDB命令使用示例
用图书的示例来说明一下mongodb命令的使用。 添加 插入一个图书文档: db.books.insert({name:深入学习MongoDB}); 然后,可以看一下是否插入成功了: db.books.find(); 打印的结果类似这样: { _id : ObjectId(4f8e8a8e7a919fd8a1a37e2d), name : 深入学习Mo
用图书的示例来说明一下mongodb命令的使用。
添加
插入一个图书文档:
db.books.insert({name:’深入学习MongoDB’});
然后,可以看一下是否插入成功了:
db.books.find();
打印的结果类似这样:
{ “_id” : ObjectId(“4f8e8a8e7a919fd8a1a37e2d”), “name” : “深入学习MongoDB” }
好了,现在不想要这条记录了,删除掉:
db.books.remove();
再次使用find(),就不会打印内容了。这里要注意,remove()将删除集合中所有文档。如何删除指定条件的文档,后面再说。
修改
再次创建一个图书文档:
db.books.insert({name:’钱的历史’,items:[]});
这里我增加了一个空数组,后面有用。
现在我要将图书名称(name)改为:深入学习MongoDB。
命令:
db.books.update({_id:ObjectId(“4f8e8a8e7a919fd8a1a37e2d”)},{name:’深入学习MongoDB’},false);
上述命令的缺点是,items属性没有了。如果只想修改图书的名称,而不影响文档的其他属性,可以这样:
db.books.update({_id:ObjectId(“4f8ea9a619bc948142e0dad5″)},{$set:{name:’Deep in MongoDB’}},false);
现在,我想往空数组里添加一些东西,比如,一个条目:
{_id:ObjectId, type:’chapter’,items:[]}
那么,命令如下:
?db.books.update({_id:ObjectId(’4f8ea9a619bc948142e0dad5′)},{$push:{items:{_id:new ObjectId(),type:’chapter’,items:[]}}},false);
看一下显示的结果:
{
“_id” : ObjectId(“4f8ea9a619bc948142e0dad5″),
“items” : [
{
"_id" : ObjectId("4f8eaf227a919fd8a1a37e2e"),
"type" : "chapter",
"items" : [ ]
}
],
“name” : “野外维生食物”
}
现在我们想在4f8eaf227a919fd8a1a37e2e的条目的数组中加入内容,怎么做呢?要用到push,它可以向指定的数组追加元素:
db.books.update({_id:ObjectId(’4f8ea9a619bc948142e0dad5′)},{$push:{items:{_id:new ObjectId(),type:’chapter’,items:[]}}},false);
现在我又发现追加的元素的type值错了,想改成section:
db.books.update({_id:ObjectId(“4f8ea9a619bc948142e0dad5″),’items._id’:ObjectId(“4f8ef10b7a919fd8a1a37e32″)},{$set:{‘items.$.type’:'section’}},false);
我是否能在items的元素的items数组中再追加元素呢?答案是肯定的:
db.books.update({_id:ObjectId(“4f8ea9a619bc948142e0dad5″),’items._id’:ObjectId(“4f8ef10b7a919fd8a1a37e32″)},{$push:{‘items.$.items’:{_id:new ObjectId(),type:’section’,items:[]}}},false);
追加后的结果类似这样:
{
“_id” : ObjectId(“4f8ea9a619bc948142e0dad5″),
“items” : [
{
"_id" : ObjectId("4f8ef10b7a919fd8a1a37e32"),
"items" : [
{
"_id" : ObjectId("4f8ef4d17a919fd8a1a37e33"),
"type" : "section",
"items" : [ ]
}
],
“type” : “chapter”
}
]
}
删除
如果文档添加了不该有的属性,想删除,比如:
db.books.update({_id:ObjectId(’4f8e8a8e7a919fd8a1a37e2d’)},{$push:{hello:’test1′}},false);
为文档增加了一个没用的属性,he l lo,这是个数组。
删除它:
db.books.update({_id:ObjectId(’4f8e8a8e7a919fd8a1a37e2d’)},{$unset:{hello:1}},false);
原文地址:MongoDB命令使用示例, 感谢原作者分享。

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

AI Hentai Generator
Generate AI Hentai for free.

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

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy
