search
HomeDatabaseMysql TutorialMongoDB下载安装测试及使用

MongoDB下载安装测试及使用

Jun 07, 2016 pm 04:06 PM
64 bitmongodbdownloaduseInstalltest

1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi http://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi?_ga=1.238525191.607472782.1411452026 32位:mongodb-win32-i386-2.6.5

1.下载安装

64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi

http://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi?_ga=1.238525191.607472782.1411452026

32位:mongodb-win32-i386-2.6.5.zip

https://fastdl.mongodb.org/win32/mongodb-win32-i386-2.6.5.zip?_ga=1.181732967.1708362836.1411364634

2.安装目录:

将应用安装到此目录下面:

D:\MongoDB\

3.新建目录

D:\MongoDB\data\db

D:\MongoDB\data\log

4.启动进城:

cd D:\MongoDB\bin

mongod -dbpath “D:\MongoDB\data\db”

\

5.测试连接

 新开一个cmd窗口,进入MongoDB的bin目录,输入mongo或者mongo.exe,出现如下信息说明测试通过,此时我们已经进入了test这个数据库,如何进入其他数据库下面会说。

\

 输入exit或者ctrl+C可退出。

6.当mongod.exe被关闭时,mongo.exe 就无法连接到数据库了,因此每次想使用mongodb数据库都要开启mongod.exe程序,所以比较麻烦,此时我们可以将MongoDB安装为windows服务

 还是运行cmd,进入bin文件夹,执行下列命令

控制台执行命令:D:\MongoDB\bin>

mongod --dbpath "D:\MongoDB\data\db" --logpath "D:\MongoDB\data\log\MongoDB.log" --install --serviceName "MongoDB"

\

 这里--MongoDB.log就是开始建立的日志文件,--serviceName "MongoDB" 服务名为MongoDB。

 接着启动mongodb服务

 > D:\MongoDB\bin>NET START MongoDB

\

 打开任务管理器,可以看到进程已经启动

7.关闭服务和删除进程

> D:\MongoDB\bin>NET stop MongoDB (关闭服务)

>D:\MongoDB\bin>mongod --dbpath "D:\MongoDB\data\db" --logpath "D:\MongoDB\data\log\MongoDB.log" --remove --serviceName "MongoDB"

(删除,注意不是--install了)

 

二、使用mongodb

1.常用的命令

show dbs 显示数据库列表

use dbname 进入dbname数据库,大小写敏感,没有这个数据库也不要紧

show collections 显示数据库中的集合,相当于表格

2.创建&新增

db.users.save({"name":"lecaf"}) 创建了名为users的集合,并新增了一条{"name":"lecaf"}的数据

db.users.insert({"name":"ghost", "age":10}) 在users集合中插入一条新数据,,如果没有users这个集合,mongodb会自动创建

save()和insert()也存在着些许区别:若新增的数据主键已经存在,insert()会不做操作并提示错误,而save() 则更改原来的内容为新内容。

存在数据:{ _id : 1, " name " : " n1 "} ,_id是主键

insert({ _id : 1, " name " : " n2 " }) 会提示错误

save({ _id : 1, " name " : " n2 " }) 会把 n1 改为 n2 ,有update的作用。

3.删除

db.users.remove() 删除users集合下所有数据

db.users.remove({"name": "lecaf"}) 删除users集合下name=lecaf的数据

db.users.drop()或db.runCommand({"drop","users"}) 删除集合users

db.runCommand({"dropDatabase": 1}) 删除当前数据库

4.查找

db.users.find() 查找users集合中所有数据

db.users.findOne() 查找users集合中的第一条数据

5.修改

db.users.update({"name":"lecaf"}, {"age":10}) 修改name=lecaf的数据为age=10,第一个参数是查找条件,第二个参数是修改内容,除了主键,其他内容会被第二个参数的内容替换,主键不能修改,如图

三、高级应用

1.条件查找

db.collection.find({ "key" : value }) 查找key=value的数据

db.collection.find({ "key" : { $gt: value } }) key > value

db.collection.find({ "key" : { $lt: value } }) key

db.collection.find({ "key" : { $gte: value } }) key >= value

db.collection.find({ "key" : { $lte: value } }) key

db.collection.find({ "key" : { $gt: value1 , $lt: value2 } }) value1

db.collection.find({ "key" : { $ne: value } }) key value

db.collection.find({ "key" : { $mod : [ 10 , 1 ] } }) 取模运算,条件相当于key % 10 == 1 即key除以10余数为1的

db.collection.find({ "key" : { $nin: [ 1, 2, 3 ] } }) 不属于,条件相当于key的值不属于[ 1, 2, 3 ]中任何一个

db.collection.find({ "key" : { $in: [ 1, 2, 3 ] } }) 属于,条件相当于key等于[ 1, 2, 3 ]中任何一个

db.collection.find({ "key" : { $size: 1 } }) $size 数量、尺寸,条件相当于key的值的数量是1(key必须是数组,一个值的情况不能算是数量为1的数组)

db.collection.find({ "key" : { $exists : true|false } }) $exists 字段存在,true返回存在字段key的数据,false返回不存在字度key的数据

db.collection.find({ "key": /^val.*val$/i }) 正则,类似like;“i”忽略大小写,“m”支持多行

db.collection.find({ $or : [{a : 1}, {b : 2} ] }) $or或 (注意:MongoDB 1.5.3后版本可用),符合条件a=1的或者符合条件b=2的数据都会查询出来

db.collection.find({ "key": value , $or : [{ a : 1 } , { b : 2 }] }) 符合条件key=value ,同时符合其他两个条件中任意一个的数据

db.collection.find({ "key.subkey" :value }) 内嵌对象中的值匹配,注意:"key.subkey"必须加引号

db.collection.find({ "key": { $not : /^val.*val$/i } }) 这是一个与其他查询条件组合使用的操作符,不会单独使用。上述查询条件得到的结果集加上$not之后就能获得相反的集合。

2.排序

db.collection.find().sort({ "key1" : -1 ,"key2" : 1 }) 这里的1代表升序,-1代表降序

3.其他

db.collection.find().limit(5) 控制返回结果数量,如果参数是0,则当作没有约束,limit()将不起作用

db.collection.find().skip(5) 控制返回结果跳过多少数量,如果参数是0,则当作没有约束,skip()将不起作用,或者说跳过了0条

db.collection.find().skip(5).limit(5) 可用来做分页,跳过5条数据再取5条数据

db.collection.find().count(true) count()返回结果集的条数

db.collection.find().skip(5).limit(5).count(true) 在加入skip()和limit()这两个操作时,要获得实际返回的结果数,需要一个参数true,否则返回的是符合查询条件的结果总数

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software