Home Database Mysql Tutorial 【MongoDB数据库】MongoDB命令入门初探

【MongoDB数据库】MongoDB命令入门初探

Jun 07, 2016 pm 03:56 PM
mongodb getting Started Preliminary exploration Order database yes

MongoDB是一款NoSql数据库,使用了“面向集合”(Collection-Oriented)原理,意思是数据被分组存储在数据集中,被称为一个集合(Collection)。每个集合(Collection)在数据库中都有一个唯一的标识名,并且可以包含无限数目的对象(BasicDBObject)。 集合(Co

MongoDB是一款NoSql数据库,使用了“面向集合”(Collection-Oriented)原理,意思是数据被分组存储在数据集中,被称为一个集合(Collection)。每个集合(Collection)在数据库中都有一个唯一的标识名,并且可以包含无限数目的对象(BasicDBObject)。

集合(Collection)的概念类似关系型数据库(RDBMS)里的表(table),对象(BasicDBObject)的概念类似于RDBMS中表的一条数据,关系数据库里插入一条数据等价于在MongoDB的Collection中添加一个BasicDBObject。

喜欢Java的人会很快喜欢上MongoDB的,因为其面向对象的操作让人眼前一亮。上一篇我们介绍了MongoDB的安装与配置过程,本篇blog以通俗简洁的方式介绍了MongoDB 入门级命令。

1、进入mongodb shell

在控制面板中开启MongoDB服务,同时使用mongo命令连接已经启动的MongoDB server,进入mongodb shell,如下图所示:

2、显示当前数据库

>db

test

3、显示所有数据库

>show dbs

admin (empty)
andyDB 0.078GB
local 0.078GB

4、切换数据库(如果该数据库不存在,则创建一个数据库)
>use andyDB

switched to db andyDB

5、显示所有的表(集合Collection)

>show collections

system.indexes

6、创建一个表person,并初始化数据

>db.person.insert({name:"andy",age:25})

WriteResult({ "nInserted" : 1 })

7、插入一条记录

>db.person.save({name:"jack",age:50})

WriteResult({ "nInserted" : 1 })

8、查询所有记录

>db.person.find()

{ "_id" : ObjectId("537761762c82bf816b34e6ce"), "name" : "andy", "age" : 25 }
{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }

9、查询某一条记录

> db.person.find({name:"jack"})

{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }

> db.person.find({$where:"this.name.length

{ "_id" : ObjectId("537761762c82bf816b34e6ce"), "name" : "andy", "age" : 25 }

{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }

10、更新一条记录

> db.person.update({name:"andy"},{$set:{age:100}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.person.find({name:"andy"})

{ "_id" : ObjectId("537761762c82bf816b34e6ce"), "name" : "andy", "age" : 100 }

11、删除一条记录

> db.person.remove({name:"andy"})

WriteResult({ "nRemoved" : 1 })

> db.person.find()

{ "_id" : ObjectId("537761da2c82bf816b34e6cf"), "name" : "jack", "age" : 50 }

>db.person.remove()//删除表中的所有记录

>db.person.drop()//删除表

12、查看帮助命令

1)db.help() 显示关于db的帮助命令

>db.help()

2)db.collection.help()显示关于Collection的帮助命令

>db.person.help()

3)db.collection.function.help()显示关于Collection方法的帮助命令

> db.person.function.help()

13、参考

MongoDB Hello World Example(推荐)

Getting Started with MongoDB

14、你可能感兴趣

【MongoDB数据库】如何安装、配置MongoDB

【MongoDB数据库】Java MongoDB CRUD Example

转载请注明出处:http://blog.csdn.net/andie_guo/article/details/26095367,谢谢!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

What is the CentOS MongoDB backup strategy? What is the CentOS MongoDB backup strategy? Apr 14, 2025 pm 04:51 PM

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

How to encrypt data in Debian MongoDB How to encrypt data in Debian MongoDB Apr 12, 2025 pm 08:03 PM

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

MongoDB and relational database: a comprehensive comparison MongoDB and relational database: a comprehensive comparison Apr 08, 2025 pm 06:30 PM

MongoDB and relational database: In-depth comparison This article will explore in-depth the differences between NoSQL database MongoDB and traditional relational databases (such as MySQL and SQLServer). Relational databases use table structures of rows and columns to organize data, while MongoDB uses flexible document-oriented models to better suit the needs of modern applications. Mainly differentiates data structures: Relational databases use predefined schema tables to store data, and relationships between tables are established through primary keys and foreign keys; MongoDB uses JSON-like BSON documents to store them in a collection, and each document structure can be independently changed to achieve pattern-free design. Architectural design: Relational databases need to pre-defined fixed schema; MongoDB supports

See all articles