Skip to content

Instantly share code, notes, and snippets.

@myctw
myctw / upsertIndex.js
Created March 15, 2022 10:40
MongoDB upsert index
/*
Please advise me better coding style and syntax, thanks!
Process Flow:
1. prepare readyToCreateIndexes which contains one or more indexes
2. get all current indexes and compare keyName and key field
- keyName
- not exist:
- fields different, insert!

MongoDB Syntax Quick List

Get All Collection Name Operation

  • Way1
db.getCollectionNames()
  • Way2
@myctw
myctw / mongorestore.sh
Created September 23, 2021 03:43
MongoDB import data script
#!/bin/bash
host=127.0.0.1
port=27017
authDB=admin
db=[YourDB]
user=root
pwd=[YourPassword]
source_folder="./MongoDBBackup/${db}/"
@myctw
myctw / mongodump_multi_collections.sh
Last active February 6, 2024 00:00
Mongodb dump multiple collections script
#!/bin/bash
host=127.0.0.1
port=27017
authDB=admin
db=[YourDB]
user=root
pwd=[YourPassword]
collections="[Collection1] [Collection2]"
output_folder="./export/"