mongo初尝试 常用语句

mongo初尝试

·
注意函数的大小写,mongoDB是严格区分大小写的!!!
注意函数的大小写,mongoDB是严格区分大小写的!!!
注意函数的大小写,mongoDB是严格区分大小写的!!!
·
ref1: MongoDB与MySQL常用操作语句对照
ref2: MongoDB聚合查询详解
·
按工作中需求记录学习
·

查看版本

# select version();
db.version();
print(db.version());

·
以表starProfile为例:
·

order

# select * from starProfile;
db.starProfile.find();

# select id from starProfile order by id desc;
db.starProfile.find({_id:ObjectId('...')}).sort({_id:1});	// 正序 asc
db.starProfile.find({_id:ObjectId('...')}).sort({_id:-1});	// 倒序 desc

·
·

where

# select * from starProfile where spider_update like '%2020-10-10%' and userId=1110395428;
db.starProfile.find({spider_update:/2020-10-10/, userId:1110395428});

·
·

delete 字段 update_at

db.starProfile.updateMany({'update_at':''}, {'$unset':{'update_at': 1}});

·
·

delete 记录

# delete from starProfile where spider_update is null;
db.starProfile.remove({spider_update:null});

·
·

删除starId重复的记录,[仅保留spider_update最靠近当前的一条,这个待完成]

# delete from myTable where id not in (select newid from (select min(id) as newid from myTable group by pk)t);

db.shiyi_starProfile.aggregate([
	{ $group:{
			_id:{starId:'$starId'},						// 以starId为重复条件查询
			count:{$sum:1},								// 计数
			dups:{$addToSet:'$spider_update'}			// 保存到桶中
	}},
	{ $match:{
		count:{$gt:1}									// 查重复数 大于 1的记录
	}}
], {allowDiskUse:true}									// 数据量过大时使用
).forEach(function(doc){
		print(doc.dups.shift())							// 弹出第一条,避免把库内数据全部移除
//		db.shiyi_starProfile.remove({_id:{$in:doc.dups}}); // 删除数据
	})
;

将str类型转为int类型

# 将 hot 为空的 填充 0
db.table_name.find({'hot': ''}).forEach(
	function(x){
        print(x.hot, x.updateTime);
        x.hot = 0;
        db.db_name.save(x);
     }
);


# 将str类型转为int类型
db.table_name.find({'hot':{'$type':2}}).forEach(
	function(x){
        print(x.hot, x.updateTime);
        x.hot = new NumberInt(x.hot);
        db.table_name.save(x);
);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值