美文网首页
mongodb 入门

mongodb 入门

作者: 佐佐木君 | 来源:发表于2019-01-23 11:45 被阅读0次

一、安装

1 brew install mongodb

2 配置bash_profile

MONGODB_HOME=/usr/local/mongodb

path=$MONGODB_HOME:$PATH:.

3 查看是否安装成功

mongod -version

4 创建目录

进入mongodb安装目录,创建data和log目录

mkdir data

mkdir log

5 执行

mongod --dbpath data --logpath log/mongod.log --logappend --fork

二、CRUD

db.getCollection("user").find({})

db.user.insertOne({item:"canvans",qty:100,tags:["cottons"],size:{h:28,w:35.5,uom:"cm"}})

db.user.insertMany([{item:"journal",qty:23,tags:["blank","red"],size:{h:14,w:12,uom:"cm"}},{item:"journal2",qty:23,tags:["blank","red"],size:{h:14,w:12,uom:"cm"}},{item:"journal3",qty:23,tags:["blank","red"],size:{h:14,w:12,uom:"cm"}}])

// query select * from table_name

db.user.find({})

//按照条件查询 db.collection.find({key:value})

db.user.find({size:{h:14,w:12,uom:"cm"}})

// 使用查询运算符指定条件 in条件

// 使用查询运算符指定条件 in条件

db.user.find({

item:{

$in:[

"journal",

"journal2"

]

}

})

// 指定and条件 SELECT * FROM inventory WHERE item = “journal” AND qty < 30

db.user.find({

item:"journal",

qty:23

})

// 指定OR条件 SELECT * FROM inventory WHERE status = “A” OR qty < 3

db.user.find({

$or:[

{

    item:"journal"

},

{

qty:100

}

]

})

// 指定AND 和 OR条件 SELECT  *  FROM  inventory  WHERE  status  =  “A”  AND  ( qty  <  30  OR  item  LIKE  “p%” )

db.user.find({

tags:["blank","red"],

$or:[

{

"item":/^journal/

}

]

})

// updateOne and updateMany

db.user.updateOne(

{

    "item":"canvans"

},

{

$set:{

qty:150,

tags:["haha","haha2"]

},

$currentDate:{

lastModified:true

}

}

)

聚合相关:

$project:修改文档的结构,可以用来重命名,增加或删除文档中的字段

eg:

db.article.aggregate([

{

$project:{_id:0,title:1,author:1}

}

])

因为字段_id是默认显示的,这里必须用_id:0 把字段过滤掉

把文档中pages字段的值都增加10,并重命名为newPages字段

db.article.aggregate(

[

{

$project:{_id:0,title:1,author;1,newPages:[$add:{"$pages",10}]}

}

]

)

$math:用于过滤文档,用法类似find()方法中的参数

db.article.aggregate([

{

$match:{"pages":{$gte:5}}

}

])

$group:将集合中的文档进行分组,可用于统计结果

db.article.aggregate([

{

$group:{id:"$author",total:{$sum:1}}

}

])

$sort:将集合中的文档进行排序

让集合article以pages升序排列

db.article.aggregate([

{

$sort:{"pages":1}

}

]).pretty()

$limit:限制集合article中前两条文档

db.article.aggregate([{$limit:2}]).pretty()

$skip:跳过指定数量的文档,并返回余下的文档

db.article.aggregate([{$skip:1}]).pretty()

$unwind:将文档中数组类型的字段拆分成多条,每条文档包含数组中的一个值。

把集合中articke=”MongoDB Aggregate“的tags字段拆分

db.article.aggregate([

{

$match:{"title":"MongoDB Aggregate"}

},

{

$unwind:"tags"

}

])

$unwind 参数数组字段为空或不存在时,待处理的文档将会被忽略,该文档将不会有任何输出

$unwind 参数不是一个数组类型时,将会抛出异常

$unwind 所作的修改,只用于输出,不能改变原文档

表达式操作符:

布尔管道聚合操作,集合操作,比较聚合操作,算数聚合操作,字符串聚合操作,数组聚合操作,日期聚合操作,条件聚合操作,数据类型聚合操作。

布尔管道:$and $or $not

{ "_id" : 1, "item" : "abc1", description: "product 1", qty: 300 }

{ "_id" : 2, "item" : "abc2", description: "product 2", qty: 200 }

{ "_id" : 3, "item" : "xyz1", description: "product 3", qty: 250 }

{ "_id" : 4, "item" : "VWZ1", description: "product 4", qty: 300 }

{ "_id" : 5, "item" : "VWZ2", description: "product 5", qty: 180 }

确定 qty 是否大于250或者小于200

db.mycol.aggregate(

   [

     {

       $project:

          {

            item: 1,

            result: { $or: [ { $gt: [ "$qty", 250 ] }, { $lt: [ "$qty", 200 ] } ] }

          }

     }

   ]

)

{ "_id" : 1, "item" : "abc1", "result" : true }

{ "_id" : 2, "item" : "abc2", "result" : false }

{ "_id" : 3, "item" : "xyz1", "result" : false }

{ "_id" : 4, "item" : "VWZ1", "result" : true }

{ "_id" : 5, "item" : "VWZ2", "result" : true }

集合操作:求集合的并集,交集,差集运算

$setEquals

$setIntersection

$setUnion

$setDifference

$setIsSubset

$anyElementTrue

$allElementsTrue

求集合的并集:

db.mycol.aggregate(

   [

     { $project: { A:1, B: 1, allValues: { $setUnion: [ "$A", "$B" ] }, _id: 0 } }

   ]

)

比较聚合操作:

$eq $gt $gte $lt $lte $ne

$cmp:Returns: 0 if the two values are equivalent, 1 if the first value is greater than the second, and -1 if the first value is less than the second.

三、索引

1. 单字段索引

db.books.createIndex({book_name:1})

2. 复合索引 顺序重要

db.books.createIndex({pricee:1,book_name:1})

3. 多键索引:在数组字段上生成的索引,用于索引存储在数组中的内容。

4. 文本索引:

db.books.createIndex({book_name:"text"})

四、mongoTemplate 整合springBoot

https://cloud.tencent.com/developer/article/1188376

五、mogodb可视化工具

studio3T

相关文章

网友评论

      本文标题:mongodb 入门

      本文链接:https://www.haomeiwen.com/subject/wyjjjqtx.html