摘要:和对象有些类似。使用数据库创建数据库会同时创建和创建集合创建删除是一种与进行互动的接口。在使用前确认正在运行。
MongDB和JSON对象有些类似。
</>复制代码
{
name: "sue",
age: 26,
status: "A",
groups: ["news", sports]
}
</>复制代码
Query with the mongo shell
使用数据库
</>复制代码
use
创建数据库(insert会同时创建myNewDB和myNewCollection)
</>复制代码
use myNewDB
DB.myNewCollection1.insert({x: 1})
创建集合
</>复制代码
db.myNewCollection2.insert( { x: 1 } )
db.myNewCollection3.createIndex( { y: 1 } )
创建view
</>复制代码
db.runCommand( { create: , viewOn: , pipeline: } )
db.runCommand( { create: , viewOn: , pipeline: , collation: } )
删除view
</>复制代码
db.collection.drop()
</>复制代码
mongo Shell
mongo Shell是一种与MongoDb进行互动的JavaScript接口。可以使用mongo shell去查询和更新数据。
在使用mongo shell 前确认mongoBb正在运行。
1.进入mongodb安装地址
</>复制代码
cd
2.启动mongo,当运行mongo不带任何参数,默认运行localhost:27017
</>复制代码
./bin/mongo
显示正在使用的数据库
</>复制代码
db
显示可使用的数据库
</>复制代码
show dbs
或者db.getSiblingDB()
插入document
</>复制代码
db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ]
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("2014-10-01T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("2014-01-16T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
)
查询集合中所有的documents
</>复制代码
db.restaurants.find()
查询(按条件查询)
</>复制代码
db.restaurants.find( { "borough": "Manhattan" } )
db.restaurants.find( { "address.zipcode": "10075" } )
大于小于
</>复制代码
db.restaurants.find( { "grades.score": { $gt: 30 } } )
db.restaurants.find( { "grades.score": { $lt: 10 } } )
逻辑与
</>复制代码
db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )
逻辑或
</>复制代码
db.restaurants.find(
{ $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] }
)
排序
</>复制代码
db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )
</>复制代码
Update data with the mongo shell
</>复制代码
db.restaurants.update(
{ "name" : "Juni" },
{
$set: { "cuisine": "American (New)" },
$currentDate: { "lastModified": true }
}
)
db.restaurants.update(
{ "restaurant_id" : "41156888" },
{ $set: { "address.street": "East 31st Street" } }
)
//批量更新
db.restaurants.update(
{ "address.zipcode": "10016", cuisine: "Other" },
{
$set: { cuisine: "Category To Be Determined" },
$currentDate: { "lastModified": true }
},
{ multi: true}
)
</>复制代码
remove data with the mongo shell
删除
</>复制代码
db.restaurants.remove( { "borough": "Manhattan" } )
//只删除一条
db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
//删除所有
db.restaurants.remove( { } )
</>复制代码
db.restaurants.drop()
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/18923.html
摘要:安装全过程环境基本情况我是在电脑下安装的系统位,这个也是导致我安装的时候出现异常提示,原因可能是的版本是位的,我应该再找一个位的,但事实上我找不到。 简述 之前讲了一些关于MongoDB的知识,出人意料的受欢迎,也让我很吃惊,所以今天打算分享一些我在自己计算机的虚拟机的centos系统下安装MongoDB的经历,希望感兴趣的你们在安装MongoDB的时候出现问题可以来看看我是怎么安装的...
摘要:安装全过程环境基本情况我是在电脑下安装的系统位,这个也是导致我安装的时候出现异常提示,原因可能是的版本是位的,我应该再找一个位的,但事实上我找不到。 简述 之前讲了一些关于MongoDB的知识,出人意料的受欢迎,也让我很吃惊,所以今天打算分享一些我在自己计算机的虚拟机的centos系统下安装MongoDB的经历,希望感兴趣的你们在安装MongoDB的时候出现问题可以来看看我是怎么安装的...
摘要:安装全过程环境基本情况我是在电脑下安装的系统位,这个也是导致我安装的时候出现异常提示,原因可能是的版本是位的,我应该再找一个位的,但事实上我找不到。 简述 之前讲了一些关于MongoDB的知识,出人意料的受欢迎,也让我很吃惊,所以今天打算分享一些我在自己计算机的虚拟机的centos系统下安装MongoDB的经历,希望感兴趣的你们在安装MongoDB的时候出现问题可以来看看我是怎么安装的...
阅读 3040·2023-04-26 00:23
阅读 3455·2021-09-13 10:28
阅读 2238·2021-08-31 14:18
阅读 2946·2019-08-30 15:54
阅读 1984·2019-08-30 15:43
阅读 1344·2019-08-29 16:56
阅读 2847·2019-08-29 14:16
阅读 2099·2019-08-28 17:51