Архив рубрики: mongodb

Install mongodb on MacOS by steps

link   link2

For example install MondoDB 2.6.12 on macOS Catalina

  1. Download, extract and move:

    wget http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.6.12.tgz

    tar xzf mongodb-osx-x86_64-2.6.12.tgz

    mv mongodb-osx-x86_64-2.6.12/ /usr/local/mongodb/

  2. Add to file ~/.zshrc this:

    export PATH="$PATH:/usr/local/mongodb/bin"

    PS: .bash_profile or .profile not worked in my case

  3. Mkdir directory for data and set rights:

    mkdir -p ~/data/db

    chown -R mongodb.mongodb ~/data/db

  4. Run MongoDB:

    mongodb --dbpath ~/data/db

drop + add + refresh indexes

db.getSiblingDB('admin').runCommand({setParameter: 1, failIndexKeyTooLong: false})
use DB

db.getCollectionNames().forEach(function(collection) { 
 indexes = db[collection].getIndexes();
 print("Indexes for " + collection + ":");
 printjson(indexes);
});

db.COLLECTION.dropIndexes()

db.getCollectionNames().forEach(function(collection) { 
  print("Indexes for " + collection + ":"); 
  db[collection].dropIndexes(); 
});

db.getCollectionNames().forEach(function(collName) { db.runCommand({dropIndexes: collName, index: "*"}); });
rake db:mongoid:create_indexes RAILS_ENV=production
db.COLLECTION.getIndexes()
db.getCollectionNames().forEach(function(collection){db[collection].reIndex()})

Indexes

About

Fix nessary indexes in code and recreate them:

rake db:mongoid:create_indexes RAILS_ENV=production

PS Before this I had to disable the errors when creating long indexes:

# mongo
MongoDB shell version:2.6.12
connecting to: test
> db.getSiblingDB('admin').runCommand({ setParameter:1, failIndexKeyTooLong:false})

Also may be needed reindex:

Also may be needed reindex:

db.getCollectionNames().forEach(function(collection){db[collection].reIndex()});

Main commands and configs

Commands:

service redis-server start
service mongodb start
service ssh --full-restart
source /usr/local/rvm/scripts/rvm

Packages:

  • AngularJS
  • BracketHighlighter
  • LESS
  • Package Control
  • SFTP
  • ColorPicker

Config:

{
 "bold_folder_labels": true,
 "caret_style": "phase",
 "color_scheme": "Packages/Color Scheme - Default/Monokai.tmTheme",
 "enable_tab_scrolling": false,
 "fade_fold_buttons": false,
 "fallback_encoding": "Cyrillic (Windows 1251)",
 "font_size": 13,
 "highlight_line": true,
 "highlight_modified_tabs": true,
 "ignored_packages":
 [
 "RubyTest",
 "SublimeREPL",
 "Vintage"
 ],
 "line_padding_bottom": 1,
 "line_padding_top": 1,
 "show_encoding": true,
 "tab_size": 2,
 "translate_tabs_to_spaces": true,
 "update_check": false,
 "word_wrap": true
}

service mongodb in Ubuntu 16

link

So I had to write one from scratch. To create one of your own follow these steps:

  1. switch to root using
    sudo su
    

or use sudo for all the following steps.

  1. create a service script (in this example the name of the service is Mongodb)
    nano /lib/systemd/system/mongodb.service
    
  2. File content should be
    [Unit]
    Description=MongoDB Database Service
    Wants=network.target
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/mongod --config /etc/mongod.conf
    ExecReload=/bin/kill -HUP $MAINPID
    Restart=always
    User=mongodb
    Group=mongodb
    StandardOutput=syslog
    StandardError=syslog
    
    [Install]
    WantedBy=multi-user.target
    

You can also download the file from here: mongodb.service
Here is a quick description of the important fields:
ExecStart - Is the command to run. Mongo installs itself under /usr/bin and the configuration file is written at /etc
User - The uid of the mongod process.
Group - The gid of the mongod process. Note that the user and group are created by the installation.

Now to start mongodb:

sudo systemctl start mongodb

To stop mongodb service use:

sudo systemctl stop mongodb

To enable mongodb on startup

sudo systemctl enable mongodb.service

If you need to refresh the services use:

 sudo systemctl daemon-reload

 

mongo set value

db.users.update( { _id: ObjectId(«54d09446afcc44142c000001″) }, { $set: { account_token: null} } );

not work service mongod start

may be:

  • errors in /var/log/mongodb/mongod.log
  • litle free disk spase (df -h)
  • after:

rm -rf /mongo-testdata/mongod.lock
[root@shilov project]# chown -R mongod.mongod /mongo-testdata
[root@shilov project]# mongod —config /etc/mongod.conf

 

restore record mongoid

 

include Mongoid::Paranoia

c = Contest.find(«541088a753bb5660ad00041e»)

claim = c.claims.where(deleted_at: { ‘$ne’ => nil }).last

claim.restore

c.claims.where(deleted_at: { ‘$ne’ => nil }).to_a

errors

tail /var/log/messages -n 1000
tail /var/log/mongodb/mongod.log -n 10000

Commands

Model.all_in | Criteria#all_in

Adds a criterion that specifies values that must all match in order to return results. The corresponding MongoDB operation is $all.

MONGOID

# Match all people with Bond and 007 as aliases.
Person.all_in(aliases: [ "Bond", "007" ])

MONGODB QUERY SELECTOR Читать далее