convert bson to csv

bsondump collection.bson > collection.csv

check member configuration (hidden, priority)


rs.conf()['members']

show collection w/o login

output=$(mongosh --quiet --authenticationDatabase admin  --host mongo-server:27017 --username username --password pass DBname  --eval "show collections")

mongo import collection from full backup to another/different database collection

#restore to source database
mongorestore --host localhost:27017 -u username -p password --authenticationDatabase admin --nsInclude=databaseEnte.collectionEnte_text --gzip --archive=/home/user/backups/db-dump_2022-07-01.gz

#then export to file
mongoexport --authenticationDatabase admin --db databaseEnte -u username -p password --port 27017 --collection collectionEnte > collectionEnte.json 

#import to another database
mongoimport --authenticationDatabase admin --db new_databaseEnte -u username -p password --port 27017 --collection collectionEnte --file collectionEnte.json

mongoexport

# without regex
mongoexport --host localhost:27017 -u username -p password --authenticationDatabase admin --db yourDBname --collection yourCollection --type csv --fields _id,status,name,code --out file.csv

# with regex
mongoexport --host localhost:27017 -u username -p password --authenticationDatabase admin --db yourDBname --collection yourCollection --type csv --fields _id,status,name,code --out file.csv --query='{"name": {"$regex": "BRI"}}'

mongodump to fullbackup gzip file

mongodump --authenticationDatabase admin --db your_main_db -u username -p pass --port=27017 --gzip --archive=/tmp/mongodb/production/db-dump_`date "+%Y-%m-%d"`.gz

restore from fullbackup gz file

mongorestore -u username -p password --authenticationDatabase admin --host localhost:27017--gzip --archive=/home/user/backups/2022-03-07T14:00:10Z_rs-production.dump.gz

restore from fullbackup gz file to some collection only

mongorestore --host=192.168.3.1 --port=27017 -u username -p password --authenticationDatabase admin --nsInclude=db_name.db_collection --gzip --archive=/tmp/db-dump_2022-08-16.gz 
{"pictures" : {$regex : "cloudfront"}}

remove some collection data

db.chapter_unlock.deleteMany({chapter:"e0daf8b9-6ef7-4b05-b090-41474b75f107",author:"fc272346-ee8b-9f21-f524-6a79d0924bd3"})

mongorestore how to

mongorestore -u username -p password --authenticationDatabase admin --host 192.168.3.1:27017 --nsInclude=your_main_db.earnings /tmp/your_main_db/earnings.bson

login

mongo --host 192.168.3.1:27017 -u username -p password --authenticationDatabase admin

change url from some collections

use databasekamu

db.post.updateMany(
  { "attachmentspicture":{$regex : "/d275rhhnq7bswz.cloudfront.net/"} },
  [{
    $set: { attachmentspicture: {
      $replaceOne: { input: "$attachmentpicture", find: "d275rhhnq7bswz.cloudfront.net, replacement: "assets-kamu.sgp1.dimongoaloceanspaces.com" }
    }}
  }]
)

db.post.updateMany(
  { "attachments.picture":{$regex : "/d275rhhnq7bswz.cloudfront.net/"} },
  [{
    $set: { "input: attachments.pictures.$[]", find: "d275rhhnq7bswz.cloudfront.net", replacement: "assets-kamu.sgp1.dimongoaloceanspaces.com" }
    }
  }]
)


db.getCollection('text').updateMany(
    {'info.addresses':'one'},
    {$set:{'info.addresses.$':'two'}}
  )

db.media.updateMany(
  { pictures:{$regex : "/aset-aku.cloudfront.com/"} },
  [{
    $set: { pictures: {
      $replaceOne: { input: "$pictures", find: "aset-aku.cloudfront.com", replacement: "aset-aku.sgp1.dimongoaloceanspaces.com" }
    }}
  }]
)

look for primary

MONGO_PRIMARY=$(mongo --quiet --port=$port -u $username -p $password --authenticationDatabase admin --eval 'rs.isMaster().primary')

check secondary

### check 
rs.conf()

### 
rs.status

### lag replication check
rs.printSecondaryReplicationInfo() 

drop some collection

#use mydb database
use mydb
#drop users collection
db.users.drop()

dump & restore one collection only

#dump
mongodump --username usernameMongo--password passwordMongo --authenticationDatabase admin --host localhost:27017 --collection top_collection --db db_app --out /tmp/

## restore
 mongorestore --drop --username usernameMongo --password passwordMongo  --authenticationDatabase admin --host localhost:27017 --collection account --db db_app  /tmp/db_app/top_collection.bson

mongodb log

#To basically get the current profiling level use:
db.getProfilingLevel()

db.getProfilingStatus()

compact all collection


#all collection
use testDbName;
db.getCollectionNames().forEach(function (collectionName) {
    print('Compacting: ' + collectionName);
    db.runCommand({ compact: collectionName });
});

#all collection that contain stat
rs.secondaryOk();
db.getCollectionNames().forEach(function (collectionName) {
    if (collectionName.includes("stat")) {
        print('Collection with "stat": '' + collectionName);
        db.runCommand({ compact: 'collectionName' });
   ' }
}); 

Count all document with filer

db.collection.countDocuments({ author: "123", amount_point_bonus: { $gt: 0 } })