Difference between revisions of "MongoDb"
From Hawk Wiki
(→Mongodb update record based on existing value) |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | ==Install mongodb on mac== | |
<pre> | <pre> | ||
+ | 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 | ||
+ | </pre> | ||
+ | |||
+ | ===Get around mac max file limit=== | ||
+ | run | ||
+ | <pre> | ||
+ | sudo launchctl limit maxfiles 1000000 1000000 | ||
+ | </pre> | ||
+ | Then add line below to ~/.bash_profile | ||
+ | <pre> | ||
+ | ulimit -n 65536 65536 | ||
+ | </pre> | ||
+ | If want to do this automatically at start up | ||
+ | add into this file /etc/launchd.conf | ||
+ | <pre> | ||
+ | limit maxfiles 1000000 1000000 | ||
+ | </pre> | ||
+ | |||
+ | edit mongo config | ||
+ | <pre>sudo vim /usr/local/etc/mongod.conf</pre> | ||
+ | |||
+ | Method2 | ||
+ | <pre> | ||
+ | echo kern.maxfiles=65536 | sudo tee -a /etc/sysctl.conf | ||
+ | echo kern.maxfilesperproc=65536 | sudo tee -a /etc/sysctl.conf | ||
+ | sudo sysctl -w kern.maxfiles=65536 | ||
+ | sudo sysctl -w kern.maxfilesperproc=65536 | ||
+ | |||
+ | echo "ulimit -n 65536 65536" >> ~/.bash_profile | ||
+ | # auto start mongodb on start up | ||
+ | echo "pgrep mongod" >> ~/.bash_profile | ||
+ | </pre> | ||
+ | |||
+ | ===Search by timestamp=== | ||
search from a certain timestamp | search from a certain timestamp | ||
+ | <pre> | ||
db.getCollection('XXX').find({t: {$gte: new ISODate("2018-01-01T21:56:47Z")}}); | db.getCollection('XXX').find({t: {$gte: new ISODate("2018-01-01T21:56:47Z")}}); | ||
+ | </pre> | ||
search last 5 minutes | search last 5 minutes | ||
+ | <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> | </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> | ||
+ | ===mongodb insertMany is 10 times faster than insertOne=== | ||
+ | I found out this the hard way. | ||
+ | But be sure to use find and insertMany as much as you can |
Latest revision as of 03:22, 8 June 2019
Contents
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
If want to do this automatically at start up add into this file /etc/launchd.conf
limit maxfiles 1000000 1000000
edit mongo config
sudo vim /usr/local/etc/mongod.conf
Method2
echo kern.maxfiles=65536 | sudo tee -a /etc/sysctl.conf echo kern.maxfilesperproc=65536 | sudo tee -a /etc/sysctl.conf sudo sysctl -w kern.maxfiles=65536 sudo sysctl -w kern.maxfilesperproc=65536 echo "ulimit -n 65536 65536" >> ~/.bash_profile # auto start mongodb on start up echo "pgrep mongod" >> ~/.bash_profile
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 } } ); } );
mongodb insertMany is 10 times faster than insertOne
I found out this the hard way. But be sure to use find and insertMany as much as you can