mongodb使用总结


1.进入linux客户端

cd /usr/local/mongodb/bin

./mongo

 

2、查看数据库
show dbs

3、切换到所用数据库

use mongo_dsmp

4、根据集合名称查询,有两种方式(可以在windows客户端上复制)

db.getCollection('CPU_BID_202004_FID0000019').find({})

db.CPU_BID_202004_FID0000019.find().pretty()

5、查看所有集合
show collections


6、创建及查看集合


db.createCollection("test999")
db.collection.createIndex(keys, options)

db.collection.createIndex({"title":1})

db.collection.drop();

db.userInfo.find({name: /mongo/});

db.getCollection('hello_test').find({desc: /SSH/}})
db.getCollection('hello_test').find({content: /551615705465450/}})

db.getCollection('hello_test').find({_id:'6449e3e7ff88e45bdf26921a'}})
db.getCollection('hello_tcp_20230505').find({_id:'6454c45bff88e45bdf2ecebc'})


客户端命令参考:
https://tsov.net/uupee/25069/


6、查询name中包含 mongo的数据

db.userInfo.find({name: /mongo/}); //相当于%% select * from userInfo where name like ‘%mongo%';


7、查询name中以mongo开头的

db.userInfo.find({name: /^mongo/}); select * from userInfo where name like ‘mongo%';

如果在一个范围中查询可以使用

{$in:["VM","STORAGE_EFS","STORAGE"]}

如果不在一个范围中的查询可以使用

{$nin:["VM","STORAGE_EFS","STORAGE"]}

db.getCollection('usages').find({"tenantId":1453580696349184000,"billingItem":{$nin:["VM","STORAGE_EFS","STORAGE"]},"startTime.dateTime":{"$gte":ISODate("2022-05-02T00:00:00Z"),"$lte":ISODate("2022-05-12T00:00:00Z")}})

db.getCollection('hello_tcp_20220905').find({"tenantId":1453580696349184000}


db.getCollection('hello_tcp_20220914').find({"additionInfo":{$ne:"{}"}})

db.getCollection('hello_tcp_20220914').find({"additionInfo":{$ne:"{}"},"applicationProtocol":"ssh","srcip":"192.120.1.4"})

 

db.getCollection('hello_tcp_20220920').distinct("dstport")

降序排序:
db.getCollection('hello_tcp_20220920').find({"additionInfo":{$ne:"{}"}}).sort({"sendPackets":-1})

字段不等于:
b.users.find({age:{$ne:20}});


分组查询参考:
http://t.zoukankan.com/Afrafre-p-10719218.html


在目的端口聚合并降序排序:

db.getCollection('hello_tcp_20220920').aggregate(
{
$group: {
"_id": "$dstport",
count: {"$sum": 1}
}
},
{"$sort":{"count":-1}}

)

db.getCollection('hello_tcp_20230223').aggregate(
{
$group: {
"_id": "$applicationProtocol",
count: {"$sum": 1}
}
},
{"$sort":{"count":-1}}

)

db.getCollection('hello_tcp_20220905').find({"applicationProtocol":ssh}
在目的端口聚合并降序排序 限制200条:
db.getCollection('hello_tcp_20220920').aggregate(
{
$group: {
"_id": "$dstport",
count: {"$sum": 1}
}
},
{"$sort":{"count":-1}} ,
{"$limit":200}

)

在目的端口聚合并查出总条数大于10000的:
db.getCollection('hello_tcp_20220920').aggregate(
{
$group: {
"_id": "$dstport",
count: {"$sum": 1}
}
},
{"$match":{"count":{"$gt":10000}}},

)



db.collection.aggregate([
{
$match: {
status: 'A'
}
},
{
$count: 'total'
},
]);


db.getCollection('hello_test').find({desc:/UDP/});

db.getCollection('hello_test').aggregate([
{
$match: {
desc: /UDP/
}
},
{
$count: 'total'
}
]);



#按照多个字段分组,比如按照州市分组,将分组字段传给$group函数的_id字段即可

{"$group":{"_id":{"state":"$state","city":"$city"}}}


# 查询部门最高工作 》10000 的部门信息
db.emp.aggregrate(
{ "$group":{
"_id":"$post",
"max_salary":{"$max":"$salary"}
}},
{"$match":{"max_salary":{"$gt":10000}}},
{"$match":{"_id":{"$ne":"teacher"}}}
)

 

 

批量删除 集合:
删除以集合名字Test开头的集合 :
db.getCollectionNames().forEach(function(c) {
if(c.match("^flowNetworkhello")) {
db.getCollection(c).drop();
}
});






查数据量:

 

db.getCollection('abnormal_test').find({}).count();

db.getSiblingDb("mongo_dsmp").BastionCmd.count();

use mongo_dsmp;
db.Bastioncmd.count();


db.getCollection('abnormal_test').find({logType:"0x29"}).count();



嵌套查询:
db.abnormal_test.find({'content.AccessProto': "TCP"});


db.hello_test.find({
    eventtime: {
       "$gte":ISODate("2023-07-25T00:00:00Z"),
        $lt:  ISODate("2023-07-25T01:00:00Z")
    },'content.AccessProto': "TCP"
}).count()


db.hello_test.find({
    eventtime: {
       "$gte":ISODate("2023-07-24T03:00:00Z"),
        $lt:  ISODate("2023-07-24T04:00:00Z")
    },'content.AccessProto': /^TCP/
}).count()

 

db.hello_test.find({
    eventtime: {
       "$gte":ISODate("2023-07-25T02:00:00Z"),
        $lt:  ISODate("2023-07-25T03:00:00Z")
    },'content.AccessProto': /TCP/,'content.ActionStatus': "网络断开"
})

 

distinct 查询:
db.hello_test713_721.distinct("content.AccessProto",{eventtime: {
       "$gte":ISODate("2023-07-20T12:00:00Z")}
    })


 

db.help();

 


设置过期时间:
给my_collection建立一个TTL索引索引,过期时间为30秒
db.my_collection.createIndex({'expire': 1}, {expireAfterSeconds: 30})

上面的这个文档(记录)将在当前时间30秒后被删除
db.my_collection.insert({'name':'张三', 'expire': new Date()})

给my_collection建立一个TTL索引索引,过期时间为0秒

db.my_collection.createIndex({'expire': 1}, {expireAfterSeconds: 0})

db.my_collection.insert({'name':'张三', 'expire': new ISODate("2023-10-10T12:00:00.000Z")})

 


集合级别的存储大小限制:
MongoDB允许在集合级别设置存储大小的限制,通过截断或删除旧数据来控制存储空间。在创建或更新集合时,使用capped: true选项可以将集合设置为“有限制大小”集合。size(可选)为固定集合指定一个最大值(以字节计)。max(可选)为固定集合指定文档的最大数量。

db.createCollection("集合名称", { capped: true, size: 100000000 , max : 1000 })




注:1.如果 collection 达到最大存储限制(size)之前达到最大文档数量(max)会删除旧文档。

      2.MongoDB 会先检查size值,然后再检查max值

 

 

 

使用指南:
http://c.biancheng.net/view/6553.html


导出数据:
cd /usr/local/mongodb/bin
./mongoexport --host 127.0.0.1:27017 -d mongo_dsmp --collection hello_tcp_20220830 -o /home/nari/tcp.json --type json
./mongoexport --host 127.0.0.1:27017 -d mongo_dsmp --collection hello_udp_20220830 -o /home/nari/udp.json --type json


// 查询语句
db.getCollection("hello_test").find({"eventtime" : { $gt : ISODate("2023-07-12 00:00:00.000") }}).limit(1000).skip(0)

// 导入shell
/usr/local/mongodb/bin/mongoimport --host localhost:27017 -d mongo_dsmp --collection hello_test --file hello_test.json --type json -u 'dsmpMongo' -p 'mongo!@#dsmp' --authenticationDatabase=mongo_dsmp -q '{"eventtime" : { $gt : ISODate("2023-07-12 00:00:00.000") }}'

 

posted on 2021-12-16 13:49  luckyna  阅读(30)  评论(0编辑  收藏  举报

导航