使用mongo命令工具操作集合数据

与 MongoDB 建立连接

mongo
# 如果设置了密码, 使用这行命令
# mongo --port 27017 -u "admin" -p "xxxxxx" --authenticationDatabase "admin"

以操作八月创建的历史数据为例

确认操作集合

show dbs;
use [数据库名称];
show tables;

确认匹配数据和数量

db.[集合名称].find({
    
    "creation_time":{
    
    $gte:ISODate("2022-08-01T00:00:00Z"),$lt:ISODate("2022-09-01T00:00:00Z")}}).count();
db.[集合名称].find({
    
    "creation_time":{
    
    $gte:ISODate("2022-08-01T00:00:00Z"),$lt:ISODate("2022-09-01T00:00:00Z")}});

操作集合数据

删除

db.[集合名称].remove({
    
    "creation_time":{
    
    $gte:ISODate("2022-08-01T00:00:00Z"),$lt:ISODate("2022-09-01T00:00:00Z")}});

更新

db.[集合名称].update({
    
    "creation_time":{
    
    $gte:ISODate("2022-08-01T00:00:00Z"),$lt:ISODate("2022-09-01T00:00:00Z")}},{
    
    $set:{
    
    "user":"xiaohon"}},false,true);

确认操作结果

db.[集合名称].find({
    
    "creation_time":{
    
    $gte:ISODate("2022-08-01T00:00:00Z"),$lt:ISODate("2022-09-01T00:00:00Z")}}).count();

最后需要断开连接

exit

猜你喜欢

转载自blog.csdn.net/hekaiyou/article/details/127207384