mogodb常用sql

1、查询所有

db.modelMapModelElement.find({})
   .projection({})
   .sort({_id:-1})
   .limit(100)

2、删除数据

db.modelMapComponentElement.drop();
db.modelMapComponentFloor.drop();
db.modelMapModelElement.drop();
db.modelMapComponent.drop();

3、查询某个值

db.modelMapComponentElement.find({"elementName":"其他块料墙面"})
   .projection({})
   .sort({_id:-1})
   .limit(100)

db.modelMapComponentElement.find({
    "mapId":"d2d4ac92-c394-48a0-ac60-002f8870a584",
    componentList:{$elemMatch: {componentId:"3rKNFlJhr40Qi1YkZK1nzp"}}
}).forEach(function(item){
    for(var i=0;i<item.componentList.length;i++){
        if(item.componentList[i].componentId == "3rKNFlJhr40Qi1YkZK1nzp"){
            print(i)
            print(item.componentList[i])
        }
    }
})

4、模糊查询

db.modelMapComponentFloor.find({"_id":/b46b83c8-5a5c-4b90-bfc1-8c8513cae281/})
   .projection({})
   .sort({_id:-1})
   .limit(100)

5、删除

db.modelMapComponent.remove({"_id":/5a952941-02c3-4023-9085-2f95c7d065a4/});

6、查询大小

db.modelMapComponent.find({
    "mapId":"20c5e925-20f9-4032-85c9-d387dfb8f44e",
    //"componentId":"15BIZ49vHDKf6PBoWe2c81"
    
}).count()

var total=0;
db.modelMapComponentElement.find({
    "mapId":"20c5e925-20f9-4032-85c9-d387dfb8f44e",
}).forEach(function(item){
    total+=item.componentList.length;
})
print(total)

var total=0;
db.modelMapComponentHouse.find({
    "mapId":"178558932749434880",
}).forEach(function(item){
    var roomList = item.roomList;
    roomList.forEach(function(room){
        var modelList = room.modelList;
        modelList.forEach(function(model){
            total+=model.componentList.length;
        });
    });
});
print(total);

6、更新脚本

db.modelMapComponent.update({"mapId":"e8a4be9c-1e8c-49d6-98a7-0cf326d97549"},{$set:{"houseName":"其他分区","roomName":"其他空间"}},false,true);

db.modelMapComponentHouse.update({"mapId":"e8a4be9c-1e8c-49d6-98a7-0cf326d97549"},
{$set:{
    "houseName":"其他分区",
    "roomList.$[].roomName":"其他空间"
}},
false,true);


    /**
     * 根据idList查找自定义属性数据
     *
     * @param idList
     * @return
     */
    @Override
    public List<ModelComponentProperties> listCustomPropertiesByIdList(List<String> idList) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").in(idList));
        //不查询固有属性数据
        query.fields().exclude("fixedPropertySets");
        return mongoTemplate.find(query, ModelComponentProperties.class);
    }

    /**
     * 根据idList查找所有数据
     *
     * @param idList
     * @return
     */
    @Override
    public List<ModelComponentProperties> selectListByIdList(List<String> idList) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").in(idList));
        return mongoTemplate.find(query, ModelComponentProperties.class);
    }

7、建索引

db.modelMapComponentFloor.createIndex({mainDataBuildingFloorId: -1});
db.modelMapComponentFloor.createIndex({mapId: -1});
db.modelMapComponentFloor.createIndex({mapId: -1, mainDataBuildingFloorId: -1})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值