您的位置:首页 > 编程语言 > Go语言

mangodb简单用法

2017-01-24 10:19 288 查看
一.什么是mongodb
基于分布式文件存储,介于关系型数据库和非关系型数据库
二.为什么用mongodb
1用json风格存储
2.文档数据库,数据字段可变
3.简单易用的查询方式,性能高,速度快
4.所有的数据类型都有索引
db.users.insertMany(
  [
     {
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites: { artist: "Picasso", food: "pizza" },
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 85, bonus: 10 }
       ]
     },
     {
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites: { artist: "Miro", food: "meringue" },
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 64, bonus: 12 }
       ]
     },
     {
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 }
       ]
     }
  ]
)
语法
1.插入名字为 Alan的数据
  db.users.insert({name:"Alan"})

2.查询年龄大于22
  db.users.find({age:{$gt: 22}})

3.查询年龄大于22 和 state="A"
  db.users.find({state:'A',age:{$gt: 22}})
4.查询年龄大于22 or state="A"
  db.users.find({$or: [{state:'A',age:{$gt: 22}}]})

5.数组查询
  db.users.find({favourites:{artist:"picasso",food:"pizza"}})
  points数组 第一列数据  ponints小于等于85
  db.users.find({'points.0.points':{$Ite: 85}})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: