MongoDB

## Links - [3t](https://studio3t.com/knowledge-base/articles/mongodb-aggregation-framework/#mongodb-project) - [runnob](https://www.runoob.com/mongodb/mongodb-aggregate.html) - [mongodb for mac](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-os-x/) ## Initialize MongoDB ### Install and Uninstall mongoDB ``` brew tap MongoDB/brew brew install mongodb-community brew uninstall mongodb-community ``` ### Check services ``` brew services list ``` ### Start services ``` brew services start mongodb-community brew services stop mongodb-community ``` ### Verify the MongoDB service ``` brew services list | grep mongodb ``` ### Connect to the shell ``` mongosh ``` ## Use of MongoDB ### show dbs ``` show dbs ``` ### Enter a database ``` use shopDB ``` ### show and drop collections ``` show collections db.products.drop() ``` ### To see which database you are in ``` > db ``` ### Insert ``` db.products.insertOne({ name: 'orange', qty: 13, type: 'fruit', price: 8 }) db.products.insertMany([ {name: 'pear', qty: 20, type: 'fruit'}, {name: 'watermelon', qty: 10, type: 'fruit'} ]) ``` ### Find ``` db.products.find() # all db.products.find({qty: 10}) # qty == 10 db.products.find({qty: 10, type: 'it'}) # and db.products.find({$or: [{name: 'apple'}, {name: 'orange'}]}) # or db.products.find({qty: {$gt: 15}}) # > db.products.find({qty: {$gte: 15}}) # >= db.products.find({qty: {$lte: 15}}) # <= ``` ### Update ``` db.products.updateMany({name: 'watermelon'},{$set: {qty: 100}}) db.products.updateMany( {name: 'mango'},{$set: {qty: 100}}, {upsert: true} ) # insert one if the document doesn't exist ``` ### Delete ``` db.products.deleteOne({type: 'stationery'}) db.products.deleteMany({type: 'stationery'}) ``` ### Sort ``` db.products.find({type: 'fruit'}).sort({name: -1}) ``` ### Index ``` db.products.createIndex({name: -1}) db.products.getIndices() db.products.dropIndex('name_-1') ``` ### Group | Expressions | Descriptions | Examples | | --------- | ------------------------------------------------------------ | ------------------------------------------------------------ | | $sum | 计算总和。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) | | $avg | 计算平均值 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) | | $min | 获取集合中所有文档对应值得最小值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) | | $max | 获取集合中所有文档对应值得最大值。 | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) | | $push | 将值加入一个数组中,不会判断是否有重复的值。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) | | $addToSet | 将值加入一个数组中,会判断是否有重复的值,若相同的值在数组中已经存在了,则不加入。 | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) | | $first | 根据资源文档的排序获取第一个文档数据。 | db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) | | $last | 根据资源文档的排序获取最后一个文档数据 | db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}]) |