To complete the requirements in this introduction, we start with the objectid of MONGO, we know that the first four bytes of Objectid are timestamps, then we can filter the records in Mongodump-q. If you do not understand the structure of the MONGO Objectid, see "_id and Objectid".
First, our use scenario is to enter a time format and then return the minimum Objectid value for that time, and then just
Running in the MongoDB shell:
function Objectidwithtimestamp (timestamp) {
Convert string date To date object (otherwise assume timestamp is a date)
if (typeof (timestamp) = = ' String ') {
timestamp = new Date (timestamp);
}
Convert Date object to hex seconds since Unix epoch
var hexseconds = Math.floor (timestamp/1000). toString (16);
Create a ObjectId with that hex timestamp
var Constructedobjectid = ObjectId (hexseconds + "0000000000000000");
Return Constructedobjectid
}
Then call the function defined above:
Db.collectionName.find ({_id: {$lt: Objectidwithtimestamp (' 2015/10/01 ')}})
Note: The top collectionname is replaced with the collection name you want to query.
The mongodump command script syntax is as follows:
>mongodump-h dbhost-d dbname-o dbdirectory-h:
Mongdb the address of the server, for example: 127.0.0.1, of course, you can also specify the port number: 127.0.0.1:27017
-D:
The database instance that needs to be backed up, for example: test
-O:
Backup data storage location, such as: C:\data\dump, of course, the directory needs to be established in advance, after the completion of the backup, the system automatically set up a test directory in the Dump directory, which contains the database instance of the backup data.
Examples of Mongodump:
$MONGO _home/bin/mongodump--port 37218-d dbname-q ' {_id ': {$gte: ObjectId ("56742d800000000000000000")}} '-O ~/backup
Note: The above Objectid ("56742d800000000000000000") can be MONGO the shell to invoke the Objectidwithtimestamp function to obtain
MongoDB Data Recovery
MongoDB uses the Mongorerstore command to recover backed-up data.
Grammar
The mongorestore command script syntax is as follows:
>mongorestore-h dbhost-d dbname--directoryperdb dbdirectory-h:
MongoDB server Address
-D:
The database instance that needs to be recovered, for example: Test, of course, this name can also be different from the backup time, such as Test2
--directoryperdb:
Where to back up your data, for example: C:\data\dump\test, why add a test here instead of a backup dump, and read the prompts yourself!
--drop:
When you restore, delete the current data, and then restore the backed-up data. That is, after the recovery, after the backup to add modified data will be deleted, carefully use Oh!
Next we execute the following command:
>mongorestore