Home Database Mysql Tutorial mongoDB安装启动对文档的增删改操作

mongoDB安装启动对文档的增删改操作

Jun 07, 2016 pm 04:04 PM
mongodb start up Install operate document

把mongodb解压缩完的bin路径加到环境变量 创建a.bat和b.bat文件: a.bat内容: mongod --dbpath F:\MongoData b.bat内容: mongo 127.0.0.1:27017/admin a.bat是启动mongodb服务器,--dbpath用来指定数据的存储路径 b.bat是启动mongo shell(即:js 引擎),admin

把mongodb解压缩完的bin路径加到环境变量

创建a.bat和b.bat文件:

a.bat内容:

mongod --dbpath F:\MongoData

b.bat内容:

mongo 127.0.0.1:27017/admin

a.bat是启动mongodb服务器,--dbpath用来指定数据的存储路径

b.bat是启动mongo shell(即:js 引擎),admin用来指定哪个数据库

启动a.bat,看到

2014-10-14T22:35:48.734+0800 [initandlisten] waiting for connections on port 270
17

说明ok了

窗口不要关,那个是mongo的服务器

再启动b.bat:

MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27017/admin

看到这个说明,mongo shell已经启动,2.6.5中间的6是偶数,代表是稳定的release版本,奇数代表开发版

一点点简单的小命令:

创建数据库:

> use foobar
switched to db foobar

此时不做任何操作或者关闭窗口,该数据库立即消失

> db.persons.insert({name:"uspcat"})
WriteResult({ "nInserted" : 1 })

插入一条记录,该persons文档就会在foobar数据库中存在

显示有哪些数据库的命令:

> show dbs
admin (empty)
foobar 0.078GB
local 0.078GB

显示有哪些集合命令:

> show collections
persons
system.indexes

查找persons文档记录的命令:

> db.persons.find()
{ "_id" : ObjectId("543d357df0b430df52a3ef24"), "name" : "uspcat" }

也可以使用findOne()查找第一条记录:

> db.persons.findOne()
{ "_id" : ObjectId("543d357df0b430df52a3ef24"), "name" : "uspcat" }

插入记录:

> db.persons.insert({name:"extjs4.0"})
WriteResult({ "nInserted" : 1 })
> db.persons.find()
{ "_id" : ObjectId("543d357df0b430df52a3ef24"), "name" : "uspcat" }
{ "_id" : ObjectId("543d370df0b430df52a3ef25"), "name" : "extjs4.0" }

更新操作:

> var p = db.persons.findOne()
> p
{ "_id" : ObjectId("543d357df0b430df52a3ef24"), "name" : "uspcat" }
> db.persons.update(p,{name:"uspcat2"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> var p = db.persons.findOne()
> p
{ "_id" : ObjectId("543d357df0b430df52a3ef24"), "name" : "uspcat2" }

可以声明var的原因,是因为mongo shell就是个js 引擎

更新操作最好是使用查询器和修改器:

> db.persons.update({name:"extjs4.1"},{$set:{age:1,name:"tom2"}});

这样把第二条记录的name改成了tom2,同时增加了age:1的Bson

删除操作:

> db.persons.remove({name:"tom2"})
WriteResult({ "nRemoved" : 1 })

ctrl+c退出mongo shell引擎

ctrl+c退出mongod服务器,Y命令终止批处理

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 Article Tags

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)

Which version is generally used for mongodb? Which version is generally used for mongodb? Apr 07, 2024 pm 05:48 PM

Which version is generally used for mongodb?

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

The difference between nodejs and vuejs

Where is the database created by mongodb? Where is the database created by mongodb? Apr 07, 2024 pm 05:39 PM

Where is the database created by mongodb?

What are the advantages of mongodb database What are the advantages of mongodb database Apr 07, 2024 pm 05:21 PM

What are the advantages of mongodb database

What does mongodb mean? What does mongodb mean? Apr 07, 2024 pm 05:57 PM

What does mongodb mean?

Where are the mongodb database files? Where are the mongodb database files? Apr 07, 2024 pm 05:42 PM

Where are the mongodb database files?

How to open mongodb How to open mongodb Apr 07, 2024 pm 06:15 PM

How to open mongodb

How to download 360 Secure Browser on your computer How to download 360 Secure Browser on your computer Apr 12, 2024 pm 01:52 PM

How to download 360 Secure Browser on your computer

See all articles