Difference between revisions of "MongoDb"

From Hawk Wiki
Jump to: navigation, search
(Search by timestamp)
(Search by timestamp)
Line 27: Line 27:
 
<pre>
 
<pre>
 
db.getCollection('XXX').find({t: {$gte: new Date(ISODate().getTime() - 1000 * 60 * 5)}})
 
db.getCollection('XXX').find({t: {$gte: new Date(ISODate().getTime() - 1000 * 60 * 5)}})
 +
</pre>
 +
 +
===Mongodb update record based on existing value===
 +
<pre>
 +
db.person.find().snapshot().forEach(
 +
    function (elem) {
 +
        db.person.update(
 +
            {
 +
                _id: elem._id
 +
            },
 +
            {
 +
                $set: {
 +
                    name: elem.firstname + ' ' + elem.lastname
 +
                }
 +
            }
 +
        );
 +
    }
 +
);
 
</pre>
 
</pre>

Revision as of 17:38, 14 March 2018

Install mongodb on mac

brew uninstall mongodb mongodb@3.2 mongodb@3.4 || true
brew install mongodb@3.6
brew unlink mongodb && brew link --force --overwrite mongodb@3.6
brew services restart mongodb@3.6
rm ~/Library/LaunchAgents/*mongodb*
ln -sfv /usr/local/Cellar/mongodb@3.6/*/*.plist ~/Library/LaunchAgents

Get around mac max file limit

run

sudo launchctl limit maxfiles 1000000 1000000

Then add line below to ~/.bash_profile

ulimit -n 65536 65536

Search by timestamp

search from a certain timestamp

db.getCollection('XXX').find({t: {$gte: new ISODate("2018-01-01T21:56:47Z")}});

search last 5 minutes

db.getCollection('XXX').find({t: {$gte: new Date(ISODate().getTime() - 1000 * 60 * 5)}})

Mongodb update record based on existing value

db.person.find().snapshot().forEach(
    function (elem) {
        db.person.update(
            {
                _id: elem._id
            },
            {
                $set: {
                    name: elem.firstname + ' ' + elem.lastname
                }
            }
        );
    }
);