Mongodb简单的操作
一、插入
One和Many可加可不加加上会显示 { "acknowledged" : true, "insertedIds" : [ ObjectId("6257cd785e88cde95125012b"), ObjectId("6257cd785e88cde95125012c") ] } 不加 WriteResult({ "nInserted" : 1 }) db.a.insertOne({ name: "haha", from: { country: "China", province: "Guangdon" } }) #插入单个的 db.a.insertMany([ {name: "haha", from: { country: "China", province: "Guangdon" } }, {name: "haha", from: { country: "China", province: "Guangdon" } } ]) #插入多条数据
二、查看数据
db.a.find({"find.name" : "haha"}) db.a.find({ "from.country" : "haha" }) #两个都可以查到信息 db.a.find( { },{from: 0} ) #from的值不会显示
三、数组查询
db.a.insert([ { "name": "Apple", color: ["red","yellow"]}, { "name": "Apple", color: ["red","green"]} ]) db.a.find({$or: [{color: "red"},{color: "green"}]})#查看数组,下面两行是结果 { "_id" : ObjectId("6257caaf5e88cde951250126"), "name" : "Apple", "color" : [ "red", "yellow" ] } { "_id" : ObjectId("6257caaf5e88cde951250127"), "name" : "Apple", "color" : [ "red", "green" ] }
四、聚合函数
$match 过滤 相当于sql的where $project 投影 相当于sql的AS $sort 排序 相当于sql的order by $group 分组 相当于sql的group by $skip/$limit 结果限制 相当于sql的SKIP/limite $lookup 做外链接 相当于sql的LEFT OUTER JOIN