资讯专栏INFORMATION COLUMN

mongodb

Hydrogen / 3018人阅读

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

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 - 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元查看
<