How to Get Exact List of Collections Form Mongo Database in Shell Script

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.

  1. 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')

  2. open a cmd in mongo/bin

  3. 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



Leave a reply



Submit