创建集合feeauditlog:db.createCollection('feeauditlog');
删除集合:db.afcfeeauditlog.drop();
插入数据:db.feeauditlog.insert({WorkOrderId:'WI11211',date:'2021',fee:105});
插入多条数据:db.feeauditlog.insertMany([
{WorkOrderId:'WI11311',date:'2021',fee:105},
{WorkOrderId:'WI11411',date:'2021',fee:105}
]);
查找feeauditlog集合中所有数据,限制两条:db.feeauditlog.find({}).limit(2);
db.feeauditlog.find({},{WorkOrderId:1,fee:1}).limit(2);--查找feeauditlog集合中WorkOrderId和fee字段,限制两条
db.feeauditlog.count({date:'2022'}); --统计date:'2022'的数据量
db.feeauditlog.findOne({date:'2022'}); --查找一条(findOne)feeauditlog集合中,date为2022的数据
db.feeauditlog.find({date:'2022'}).sort({fee:-1}); -- 查找feeauditlog集合中,date为2022的数据,按照fee降序(-1)排序(sort({fee:-1}))
db.feeauditlog.find({date:{$gte:'2022'}}); --查找feeauditlog集合中,date大于等于($gte)2022的数据
小于$lt
小于或等于$lte
大于$gt
大于或等于$gte
不等于$ne
db.workingjsoninfo.renameCollection("workjsoninfo15"); --重命名集合
db.workingjsoninfo.find({}).count();--统计数量
db.feeauditlog.find({date:{$gte:'2022'},fee:107}); --查找date大于等于2022,且fee=105
db.feeauditlog.find({
$or:[
{date:{$gte:'2021'}},{fee:104}
]
}); --查找date大于等于2022,或fee=105
db.feeauditlog.find({
$or:[
{date:{$gte:'2022'}},{fee:104}
],
WorkOrderId:'WI11211'
}); --查找date大于等于2022,或fee=105,且WorkOrderId=WI11211
db.feeauditlog.updateMany({WorkOrderId:'WI11211'},{$set:{date:'2022',fee:107}}); --刷新所有WorkOrderId为WI11211的数据的date和fee字段
db.feeauditlog.remove({}); --删除全部
db.feeauditlog.remove({WorkOrderId:'WI11111'}); --删除匹配数据
db.feeauditlog.remove({WorkOrderId:'WI11111'},{justOne:true}); --仅删除一条{justOne:true}匹配数据
db.getCollection('feeauditlog').getIndexes(); --查看索引
db.getCollection('feeauditlog').createIndex( {"WorkOrderId": 1}, {"name":"NON_WorkOrderId"} ); --创建索引#1代表升序,-1代表降序,name指定索引名
db.getCollection('feeauditlog').dropIndex( "NON_WorkOrderId" ); --删除索引
update数据“null”的字符串为null:
db.workjsoninfo.updateMany({'WorkflowInfo':"null"},{$set:{WorkflowInfo:null}});
db.workjsoninfo.updateMany({'WorkflowInfo.DoneAt':"null"},{$set:{'WorkflowInfo.DoneAt':null}});
//forEach(function(代指,数组索引,数组){#操作}) mongodb循环
db.workjsoninfo.find().forEach(
function(item){
item.WorkflowInfo.forEach(
function(time,index){
item.WorkflowInfo[index].DoneAt= ISODate(item.WorkflowInfo[index].DoneAt);
item.WorkflowInfo[index].StartedAt= ISODate(item.WorkflowInfo[index].StartedAt);
item.WorkflowInfo[index].InsertTime= ISODate(item.WorkflowInfo[index].InsertTime);
}
);
db.workjsoninfo.save(item);
}); --遍历数组元素,把字符串刷成ISODate格式时间
db.workjsoninfo.find({}).forEach(function (el){el.OperatedInfo=JSON.parse(el.OperatedInfo);db.workjsoninfo.save(el)});--把字符串刷为数组格式
//设置result,循环刷新--把一个集合的数据刷新到另外的集合
var result = db.workingjsoninfo.find({})
while(result.hasNext()) db.workjsoninfo13.insert(result.next())
;
//查询并统计数据
db.workingjsoninfo_pre.find({$and:[
{$or:[{ProName:'北京市'},{ProName:'西藏自治区'},{CityName:'石家庄市'},{AreaName:'槐荫区'}]},
{$or:[{WorkStatus:3},{WorkStatus:4},{WorkStatus:6},{WorkStatus:13},{WorkStatus:14},{WorkStatus:15},{WorkStatus:17},{WorkStatus:12}]},
{AFCRetreatTime:{$gte:new Date()}}
]
}).count();
//查询指定类型数据
db.workjsoninfo.find({"WorkAddressInfo.LocationLat":{$type:1}}) //查double类型