mongodb数据库基本操作
一般来说,涉及到mongodb的操作主要有四种:增删查改。 nodejs可以很方便简洁的实现这些操作。 准备工作: ①、连接mongodb服务器 var SERVER = new mongodb.Server( 'localhost' , 27017 , {auto_reconnect: true }); 这里SERVER就指本地(localhost)的服
一般来说,涉及到mongodb的操作主要有四种:增删查改。
nodejs可以很方便简洁的实现这些操作。
准备工作:
①、连接mongodb服务器
<code class=" hljs cs"><span class="hljs-keyword">var</span> SERVER = <span class="hljs-keyword">new</span> mongodb.Server(<span class="hljs-string">'localhost'</span>, <span class="hljs-number">27017</span>, {auto_reconnect:<span class="hljs-keyword">true</span>});</code>
这里SERVER就指本地(localhost)的服务器
②、连接服务器上的数据库
<code class=" hljs cs"><span class="hljs-keyword">var</span> DB = <span class="hljs-keyword">new</span> mongodb.Db(<span class="hljs-string">'users'</span>, SERVER, {safe:<span class="hljs-keyword">true</span>});</code>
SERVER指上面的数据库所在服务器,这里DB指SERVER上的user这个数据库。
③、打开数据库。
上面的连接好了以后,我们就可以打开数据库了。打开方式如下:
<code class=" hljs javascript">DB.open(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(err, db)</span>{</span> <span class="hljs-keyword">if</span> (err) { <span class="hljs-comment">// throw err</span> } <span class="hljs-keyword">else</span> { <span class="hljs-comment">// 打开成功, db就是打开的数据库,接下来“增删改查”的操作都对这个db来就可以了</span> } }</code>
当然,我们一般不是直接对于一个数据库整体进行操作,而是针对其中的某一个collection来操作的,那么可以这样:
<code class=" hljs cs"><span class="hljs-keyword">var</span> Xcollection = db.collection(<span class="hljs-string">"userslist"</span>);</code>
这样Xcollection就指向了上面db数据库中的”userslist”这个collection了。
但是,如果是第一次使用这个collection,可以输入mongo命令创建一个名为“userslist”的collection,或者用如下的方法新建之并继续操作:
<code class=" hljs javascript">db.createCollection(<span class="hljs-string">'userslist'</span>, {safe:<span class="hljs-literal">true</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(err, Xcollection)</span>{</span> <span class="hljs-comment">//同样在这里可以操作Xcollection,和上面意义是一样的。</span> }</code>
使用db.createCollection(),如果该collection已存在,那么就仅仅是打开而已。
++++++++++++++++++++++++++++++++++++++++++
1、增
向collection中增加一个user对象(像{“a”:”b”}这种格式),可以这样来做:
<code class=" hljs javascript">collection.insert(user, {safe:<span class="hljs-literal">true</span>}, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(err, result)</span>{</span> <span class="hljs-comment">// result是一个对象,表示插入数据的结果</span> })</code>
2、查
在collection中,查询一个user对象可以这样来做:
<code class=" hljs javascript">collection.find(user).toArray(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(err, items)</span> {</span> <span class="hljs-comment">//items是一个对象数组,从0开始编号,items.length表示查找到的对象个数</span> })</code>
不过这里需要注意,查询出来的所有结果userX,并不一定是与user完全相等,只要userX中包含了user所有的键值对并且值都相等,那么userX就会被包含在items[]中。
3、改
待续
4、删
待续, too
诶,说的都是最浅显的内容了,更全面的还是看官方文档吧。

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

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.

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:

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.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

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.

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.

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea
