How to get exact list of collections form mongo database in shell script
use --quiet
flag
collections=mongo $dbName --quiet --eval "db.getCollectionNames()"
MongoDB dump all collections in a database with query
I finally solved the problem with a navie implementation.
first generate a dumpcommond.txt file in python with regular expression.
command_tick = 'mongodump' + ' --db ' + HOST_TICK_DB + ' --collection ' + contract + ' --out tick_remote_1 ' + \
' --query "{ datetime: {$gte: new Date(' + ds_time + '), $lt: new Date(' + de_time + ')}}"'
with open(MONGODB_BIN_PATH + '\dumpcommond.txt','a') as myfile:
myfile.write(command_tick + '\n')open a cmd in mongo/bin
- copy and past all the commands in dumpcommond.txt the cmd to get all collections
Mongo Shell and .js script to rename collection by filtering part of name and comparing it to a var
You can write an admin script based on your outline as follows:
var newVersion = passwordPrompt();
var collectionsWithWordOnly = name => name.startsWith('CollectionUpgrade');
var olderVersionsOnly = name => {
var [_, version, old] = name.split('_');
if (old) {
return false;
}
if (+version <= +newVersion) {
return false;
}
return true;
};
var renameAsOldVersion = (name) => {
var newName = `${name}_old`;
db[name].renameCollection(newName);
};
db.getCollectionNames()
.filter(collectionsWithWordOnly)
.filter(olderVersionsOnly)
.forEach(renameAsOldVersion);
Access MONGO collections with weird characters from shell, Part II
This collection can still be accessed using method notation.
db.getCollection("_sys/config").count();
Credit to Ben Frank for finding this.
Retrieve only the queried element in an object array in MongoDB collection
MongoDB 2.2's new $elemMatch
projection operator provides another way to alter the returned document to contain only the first matched shapes
element:
db.test.find(
{"shapes.color": "red"},
{_id: 0, shapes: {$elemMatch: {color: "red"}}});
Returns:
{"shapes" : [{"shape": "circle", "color": "red"}]}
In 2.2 you can also do this using the $ projection operator
, where the $
in a projection object field name represents the index of the field's first matching array element from the query. The following returns the same results as above:
db.test.find({"shapes.color": "red"}, {_id: 0, 'shapes.$': 1});
MongoDB 3.2 Update
Starting with the 3.2 release, you can use the new $filter
aggregation operator to filter an array during projection, which has the benefit of including all matches, instead of just the first one.
db.test.aggregate([
// Get just the docs that contain a shapes element where color is 'red'
{$match: {'shapes.color': 'red'}},
{$project: {
shapes: {$filter: {
input: '$shapes',
as: 'shape',
cond: {$eq: ['$$shape.color', 'red']}
}},
_id: 0
}}
])
Results:
[
{
"shapes" : [
{
"shape" : "circle",
"color" : "red"
}
]
}
]
Related Topics
Redirecting Output of a C Program to Another C Program with a Bash Script Under Linux
How to Programmatically Know If I Am in a Vm
How Is The Linux Calculating Memfree
Linking with 32Bit Libraries Under Linux 64Bit
How to Ensure Data Reaches Storage, Bypassing Memory/Cache/Buffered-Io
Kernel Preemption While Holding Spinlock
Building Subversion 1.5.4 on Debian: Could Not Find Library Containing Rsa_New
Linux Shell Script - Find All Files and Run a Command on Each One of Them
Generic Printing Using a Usb Port
Different Pulic Key for Jump Host and Destination Host Over Ssh
Linux Script Telnet Head Request
Would Gcc 4.8 and 4.7 Peacefully Coexist on The Same Machine
Linux Kill Process Using Timeout in Milliseconds
How to Use Multiple Threads for Zlib Compression (Same Input Source)