Hot answers tagged

163

A Replica-Set means that you have multiple instances of MongoDB which each mirror all the data of each other. A replica-set consists of one Master (also called "Primary") and one or more Slaves (aka Secondary). Read-operations can be served by any slave, so you can increase read-performance by adding more slaves to the replica-set (provided that your client ...


125

Add --forceTableScan Example: mongodump --forceTableScan -d database_name -o target_directory http://aug2uag.blogspot.com/2018/08/mongoexport-unrecognized-field-snapshot.html The Error comes from the mongo snapshot functionality which was introduced in mongo 4.0 (https://docs.mongodb.com/manual/reference/read-concern-snapshot/) If you use an older mongo ...


103

To restore a single database you need to provide the path to the dump directory as part of the mongorestore command line. For example: # Backup the training database mongodump --db training # Restore the training database to a new database called training2 mongorestore --db training2 dump/training The --db option for mongodump specifies the source ...


63

Easiest way to find the storage engine being used currently. Inside mongo console type db.serverStatus().storageEngine It returns the storage engine being used currently { "name" : "wiredTiger" } Once it is confirmed that wiredTiger is being used then type db.serverStatus().wiredTiger to get all the configuration details of wiredTiger.


50

You can use HomeBrew, see the official MongoDB HomeBrew page. Just add the tap: brew tap mongodb/brew And then after adding the tap from above, you can then install the Mongo shell with: brew install mongodb-community-shell


48

Here are several examples of YAML configs for Linux (Windows paths and options are a little different), essentially explicitly setting some defaults and commonly used settings. First, a standalone mongod with the default port, path, journal settings - this would be the type of configuration used for local testing, with a few extras so show the general style:...


44

If you have locked yourself out then you need to do the following: Stop your MongoDB instance Remove the --auth and/or --keyfile options from your MongoDB config to disable authentication Start the instance without authentication Edit the users as needed Restart the instance with authentication enabled


44

Found answer here: https://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript db.system.profile.find({ "timestamp" : { $lt: new Date(), $gte: new Date(new Date().setDate(new Date().getDate()-1)) } })


35

The '...AnyDatabase' roles should be added to the admin database for the user: use admin db.createUser({ user: "mongoadmin" , pwd: "mongoadmin", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]}) Any database: Database User Roles Database Administration Roles Admin database: Cluster Administration Roles Backup and ...


32

Okay, so after following the clues given by loicmathieu and jstell, and digging it up a little, these are the things I found out about MongoDB using WiredTiger storage engine. I'm putting it here if anyone encountered the same questions. The memory usage threads that I mentioned, all belonged to 2012-2014, all pre-date WiredTiger and are describing behavior ...


31

Yes.. Easily! mongo "mongodb://test:testpwd@localhost/test?authSource=admin" MongoDB shell version v3.4.6 connecting to: mongodb://test:testpwd@localhost/test?authSource=admin MongoDB server version: 3.4.6 MongoDB> db test MongoDB> You put your destination DB at end of "base" string and add authentication db to parameter authSource


26

Personally, I prefer the mmapv1 storage engine as of now for three reasons. Reason 1: Maturity It isn't that WiredTiger is immature. But mmapv1 is well understood and battle tested all way up and down, back and forth and above and beyond. WiredTiger has had some serious issues (see http://jira.mongodb.com for details) fairly recently, and I am not willing ...


25

By default, queries return all fields in matching documents. If you need all the fields, returning full documents is going to be more efficient than having the server manipulate the result set with projection criteria. However, using projection to limit fields to return from query results can improve performance by: removing unneeded fields from query ...


25

You certainly have a difference of version between mongodump vs your mongoDB server ( 3.6 vs 4.0). You can use docker to the rescue : docker run --rm -v $(pwd):/workdir/ -w /workdir/ mongo:4.0 mongodump -h server -d $database --out /workdir/dump/ With docker you can use a precise version of tools cli without install it. You can even specify an alias (...


23

With the need of 10TB to be backed up this gets a bit complicated. Replicas are no replacement for proper backups While delayed replica set members can provide an relatively easy way to help you with accidental operations, there are no replacement for proper backups, very much like RAID isn't a replacement for file system based backups. Recommendations ...


22

It is just informing that mongod has chosen to use the _id index. More specifically: IDHACK means that normal query plan path (evaluation, caching, re-evaluation) was bypassed for the query shape {_id: <value>} which is supported by the required _id index. This is a performance optimization for the common use case of fetching a document by primary ...


20

Use the following command to restore mongo db: mongorestore -d dbname dbpath


20

All data dumped via mongodump has to be read into memory by the MongoDB server. It's also worth noting that mongodump backs up data and index definitions; the time to restore can also be significantly longer as compared to other approaches since mongorestore will need to recreate any secondary indexes after the data is loaded. As noted in the MongoDB ...


20

Nothing works for me but this. mongorestore --gzip --archive=/path/to/file.gz --db db_name


20

mongodump generates binary copies of data; it creates better, more efficient backups. mongoexport can create JSON files; these can be used by other programs, and are basically human-readable as is.


18

The answer from Kalhara is correct if you know the position of the embedded document in your array, however there is an alternative approach that can be used to update the first matching array element without knowing the position. Assuming you update with a single array in your query criteria, the positional operator ($) can be used to refer to the matching ...


17

I had a similar issue when I created a user without adding a superuser first. The following steps helped solve the problem: Open the MongoDB configuration file using sudo (sudo vi mongodb.conf). The file can be found in /etc/ folder. Comment "auth = true". Stop the MongoDB service (sudo service mongod stop) Start the MongoDB service (sudo service mongod ...


17

With 3.2.x you cannot use --nsFrom or --nsTo parameters. This pair of commands should work in all versions: mongodump --db ${DB_NAME} --gzip -o ${BACKUP_FILE_GZ} mongorestore --gzip --db "${DB_NAME_RESTORE}" ${BACKUP_FILE_GZ}/${DB_NAME} Now you get a directory with gzipped files and you can restore all (or just one) collections to a different database.


16

This can be a little tricky, but the fact that the MongoDB shell is basically a Javascript interpreter gives us decent options in terms of filtering. Here is the function I use to accomplish this: // kills long running ops in MongoDB (taking seconds as an arg to define "long") // attempts to be a bit safer than killing all by excluding replication related ...


15

As at MongoDB 3.2, there is no feedback on the reason document validation failed: the overall validation expression currently evaluates as either True ("OK") or False ("Document failed validation"). Validation behaviour can be adjusted with validationAction (error/warn) and validationLevel (strict/moderate/off) configuration options, but this does not ...


14

If you use a replica set with a keyfile Security between members of the replica set using Internal Authentication Keyfile is used for auth in replication. security: keyFile: <path-to-keyfile> replication: replSetName: <replicaSetName> You can use this command to login as a admin to mongod: mongo -u __system -p "$(tr -d '\011-\015\040'...


14

The easy, albeit a bit unsecure way Stop the first secondary Delete the content of it's dbpath Restart the secondary Wait for it to catch up with the primary Repeat process with the second secondary This is a bit unsecure as it is unknown why the secondaries entered the Recovering state. The more secure, but also more intrusive way As above, but stop ...


14

Connecting to a replica set Most drivers will show the following behavior, as far as I know If only a single node is given, the driver will simply connect to this node. If the replicaSet option is given in the connection string, the driver will start to explore said replica set to identify all non-hidden members, regardless of the number of nodes given in ...


14

First of all, you can use a pipe mongodump -h sourceHost -d yourDatabase … | mongorestore -h targetHost -d yourDatabase … This reduces the time, as each document read will more or less instantly be restored on targetHost. However, this has the disadvantage that you might run into problems if the procedure is aborted for some reason, for example of a ...


14

these indexes are existing in collection : db.products.createIndex({"origins_tags": 1,"sortkey": -1}, { background: true }) db.products.createIndex({"last_modified_t": -1}, { background: true }) The MongoDB query optimizer chooses the most efficient query plan based on the query shape (combination of query predicate, sort, and projection) and candidate ...


Only top voted, non community-wiki answers of a minimum length are eligible