一、库表操作
1、创建表
db.demo.insertOne({companyId:"61e7654361a9b943f7ef4c40"})
或
db.createCollection("demo");
2、修改表
新增字段
db.demo.updateMany({}, {$set:{age:18}})
删除字段
db.demo.updateMany({},{$unset: {"age": ""}})
创建索引
db.demo.createIndex({companyId:1})
删除索引
db.demo.createIndex({companyId: -1})
3、删除表
db.demo.drop();
db.getCollection("demo").drop();
4、插入数据
db.demo.insertOne({companyId:"61e7654361a9b943f7ef4c40"});
db.demo.insertMany([{companyId:"61e7654361a9b943f7ef4c40"}, {companyId:"61e7654361a9b943f7ef4c41"}]);
db.demo.insert([{companyId:"61e7654361a9b943f7ef4c42"}, {companyId:"61e7654361a9b943f7ef4c43"}]);
5、查询
db.demo.find({companyId:"61e7654361a9b943f7ef4c40"});
db.demo.find({startFlag:false},{companyId:1, deptId:1, groupId:1, staffId:1, staffName:1, startFlag:1, statuses:1, _id:0});
db.demo.find({$or:[{staffId:"61e7654361a9b943f7ef4c41"}, {startFlag:true}]});
db.demo.find({requireTime:{$gt:"09:00"}});
db.demo.find({requireTime:{$gte:"09:00"}});
db.demo.find({requireTime:{$lt:"09:00"}});
db.demo.find({requireTime:{$lte:"09:00"}});
db.demo.find({requireTime:{$gt:"09:00", $lt:"18:00"}});
db.demo.find({startFlag:{$ne:false}});
db.demo.find({staffName:/莉/});
db.demo.find({staffName:{$regex:/莉/}});
db.demo.find({status: "A"}).sort({ age: 1 })
db.people.find({status: "A"}).sort({ age: -1})
db.demo.count();
db.demo.find().count();
db.demo.distinct('companyId');
db.demo.findOne();
db.demo.find().limit(1);
db.demo.aggregate([{$group:{_id:"$companyId", count:{$sum:1}}}])