MongoDB常用操作语句(持续整理)

1、查询语句find
db.getCollection(“表名”).find()
查询全部记录,相当于select * from ‘表名’;

db.getCollection(“表名”).find({‘字段名1’:‘值1’},{‘字段名2’,1})
查询表中字段名1的值为值1的记录中的字段名2属性,相当于 select ‘字段名2’ from ‘表名’ where ‘字段名1’ = ‘值1’;

2、查询统计count
查询语句后加上count()
db.getCollection(“表名”).find().count()
查询全部表中记录的个数,相当于 select count as totalcount from ‘表名’

3、更新数据set
db.getCollection(“表名”).update({‘字段名1’:‘值1’,{$set:{‘字段名2’:‘值2’}}})
将字段名1为值1的记录的字段名2(可以是字段名1)的值修改为值2,相当于 update ‘表名’ set ‘字段名2’ = ‘值2’ where ‘字段名1’=‘值1’

db.getCollection(“表名”).find({‘字段名1’:‘值1’}).update({‘字段名2’:‘值2’},{$set:{‘字段名3’:‘值3’}})
将字段名1为值1的记录中字段名2为值2的记录的字段名3的值修改为值3

批量修改
db.getCollection(“表名”).find({‘字段名1’:‘值1’}).forEach(
function(item){
db.getCollection(“表名”).update({‘id’:item.id},{$set:{‘字段名2’:‘值2’}})
})
将表中所有字段名1为值1的记录的字段名2的值修改为值2

4、删除数据unset
db.getCollection(“表名”).update({‘字段名1’:‘值1’},{$unset:{‘字段名2’:’’}})
将表中字段名1为值1的记录的字段名2删除

5、条件符号
> : $gt >= : $gte < : $lt <= : $lte != : $ne

解释一下这段代码def price(request): if request.method == "GET": contact_list = models.zufang.objects.values_list().order_by('id') contact_list = [i for i in contact_list] new_contact_list = random.sample(contact_list, 3) user_name = models.Collection.objects.get(name=request.session.get('user_name')) if user_name.count > 0: collection = user_name.collection collection = collection.split(",") collection = [int(i) for i in collection] find_index = [] for item in collection: find_index.append(models.zufang.objects.values_list().filter(id=item)[0]) find_index = [i for i in find_index] else: collection = [] find_index = [] if request.GET.get("update_item") == "ok": return JsonResponse({"new_contact_list": new_contact_list, "find_index": find_index}, safe=False) return render(request, 'price.html', locals()) else: user_name = models.Collection.objects.get(name=request.session.get('user_name')) count = user_name.count if user_name.count > 0: collection = user_name.collection collection = collection.split(",") else: collection = [] if request.POST.get("new") == "true": count += 1 new_collection = request.POST.get("data_id") collection.append(new_collection) collection = ','.join(collection) new = models.Collection.objects.get(name=request.session.get('user_name')) new.collection = collection new.count = count new.save() else: count -= 1 new_collection = request.POST.get("data_id") collection.remove(new_collection) collection = ','.join(collection) new = models.Collection.objects.get(name=request.session.get('user_name')) new.collection = collection new.count = count new.save() return JsonResponse({}, safe=False)
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值