资讯专栏INFORMATION COLUMN

mongodb

Hydrogen / 3230人阅读

摘要:和对象有些类似。使用数据库创建数据库会同时创建和创建集合创建删除是一种与进行互动的接口。在使用前确认正在运行。

MongDB和JSON对象有些类似。

</>复制代码

  1. {
  2. name: "sue",
  3. age: 26,
  4. status: "A",
  5. groups: ["news", sports]
  6. }

</>复制代码

  1. Query with the mongo shell

使用数据库

</>复制代码

  1. use

创建数据库(insert会同时创建myNewDB和myNewCollection)

</>复制代码

  1. use myNewDB
  2. DB.myNewCollection1.insert({x: 1})

创建集合

</>复制代码

  1. db.myNewCollection2.insert( { x: 1 } )
  2. db.myNewCollection3.createIndex( { y: 1 } )

创建view

</>复制代码

  1. db.runCommand( { create: , viewOn: , pipeline: } )
  2. db.runCommand( { create: , viewOn: , pipeline: , collation: } )

删除view

</>复制代码

  1. db.collection.drop()

</>复制代码

  1. mongo Shell

mongo Shell是一种与MongoDb进行互动的JavaScript接口。可以使用mongo shell去查询和更新数据。
在使用mongo shell 前确认mongoBb正在运行。

1.进入mongodb安装地址

</>复制代码

  1. cd

2.启动mongo,当运行mongo不带任何参数,默认运行localhost:27017

</>复制代码

  1. ./bin/mongo

显示正在使用的数据库

</>复制代码

  1. db

显示可使用的数据库

</>复制代码

  1. show dbs
  2. 或者db.getSiblingDB()

插入document

</>复制代码

  1. db.restaurants.insert(
  2. {
  3. "address" : {
  4. "street" : "2 Avenue",
  5. "zipcode" : "10075",
  6. "building" : "1480",
  7. "coord" : [ -73.9557413, 40.7720266 ]
  8. },
  9. "borough" : "Manhattan",
  10. "cuisine" : "Italian",
  11. "grades" : [
  12. {
  13. "date" : ISODate("2014-10-01T00:00:00Z"),
  14. "grade" : "A",
  15. "score" : 11
  16. },
  17. {
  18. "date" : ISODate("2014-01-16T00:00:00Z"),
  19. "grade" : "B",
  20. "score" : 17
  21. }
  22. ],
  23. "name" : "Vella",
  24. "restaurant_id" : "41704620"
  25. }
  26. )

查询集合中所有的documents

</>复制代码

  1. db.restaurants.find()

查询(按条件查询)

</>复制代码

  1. db.restaurants.find( { "borough": "Manhattan" } )
  2. db.restaurants.find( { "address.zipcode": "10075" } )

大于小于

</>复制代码

  1. db.restaurants.find( { "grades.score": { $gt: 30 } } )
  2. db.restaurants.find( { "grades.score": { $lt: 10 } } )

逻辑与

</>复制代码

  1. db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )

逻辑或

</>复制代码

  1. db.restaurants.find(
  2. { $or: [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] }
  3. )

排序

</>复制代码

  1. db.restaurants.find().sort( { "borough": 1, "address.zipcode": 1 } )

</>复制代码

  1. Update data with the mongo shell

</>复制代码

  1. db.restaurants.update(
  2. { "name" : "Juni" },
  3. {
  4. $set: { "cuisine": "American (New)" },
  5. $currentDate: { "lastModified": true }
  6. }
  7. )
  8. db.restaurants.update(
  9. { "restaurant_id" : "41156888" },
  10. { $set: { "address.street": "East 31st Street" } }
  11. )
  12. //批量更新
  13. db.restaurants.update(
  14. { "address.zipcode": "10016", cuisine: "Other" },
  15. {
  16. $set: { cuisine: "Category To Be Determined" },
  17. $currentDate: { "lastModified": true }
  18. },
  19. { multi: true}
  20. )

</>复制代码

  1. remove data with the mongo shell

删除

</>复制代码

  1. db.restaurants.remove( { "borough": "Manhattan" } )
  2. //只删除一条
  3. db.restaurants.remove( { "borough": "Queens" }, { justOne: true } )
  4. //删除所有
  5. db.restaurants.remove( { } )

</>复制代码

  1. db.restaurants.drop()

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/18923.html

相关文章

  • 聊聊MongoDB - MongoDB的简单安装

    摘要:安装全过程环境基本情况我是在电脑下安装的系统位,这个也是导致我安装的时候出现异常提示,原因可能是的版本是位的,我应该再找一个位的,但事实上我找不到。 简述 之前讲了一些关于MongoDB的知识,出人意料的受欢迎,也让我很吃惊,所以今天打算分享一些我在自己计算机的虚拟机的centos系统下安装MongoDB的经历,希望感兴趣的你们在安装MongoDB的时候出现问题可以来看看我是怎么安装的...

    notebin 评论0 收藏0
  • 聊聊MongoDB - MongoDB的简单安装

    摘要:安装全过程环境基本情况我是在电脑下安装的系统位,这个也是导致我安装的时候出现异常提示,原因可能是的版本是位的,我应该再找一个位的,但事实上我找不到。 简述 之前讲了一些关于MongoDB的知识,出人意料的受欢迎,也让我很吃惊,所以今天打算分享一些我在自己计算机的虚拟机的centos系统下安装MongoDB的经历,希望感兴趣的你们在安装MongoDB的时候出现问题可以来看看我是怎么安装的...

    whatsns 评论0 收藏0
  • 聊聊MongoDB - MongoDB的简单安装

    摘要:安装全过程环境基本情况我是在电脑下安装的系统位,这个也是导致我安装的时候出现异常提示,原因可能是的版本是位的,我应该再找一个位的,但事实上我找不到。 简述 之前讲了一些关于MongoDB的知识,出人意料的受欢迎,也让我很吃惊,所以今天打算分享一些我在自己计算机的虚拟机的centos系统下安装MongoDB的经历,希望感兴趣的你们在安装MongoDB的时候出现问题可以来看看我是怎么安装的...

    diabloneo 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<