GithubHelp home page GithubHelp logo

mongo-basics's Introduction

Official mongodb documentation

Run and connect mongodb server

Once you have mongodb installed you can run mongodb server with comand

mongod

In some UNIX systems the folder /data/db must be creates or you can specify another folder to store mongodb databases.

mongod --dbpath Documents/db

For more options you can run mongo options you can run

mongo -h

Once you have mongodb running then you can run mongo command in your terminal, by default you'll be connected to localhost using port 27017

mongo
mongo use databaseName

If you want to connect to a remote mongodb server then you have to specify host, port, user, password and database name.

mongo <host>:<port>/<database> -u <dbuser> -p <dbpassword>

eg:

mongo ds159033.mlab.com:59033/example-db -u read-user -p securepsw

CRUD Operations

To insert you can specify a sinlge object or an array of objects, eg:

db.collectionName.insert(document || document[]);
db.collectionName.insertOne(domcument);
db.collectionName.insertMany(document[]);
db.collectionName.save(document);

To read a collection you can filter and project the find function, eg:

db.collectionName.find(where, projection);

Projection is the set of field to include/exclude in the cursor

To update documents in a collection you can use the updates methods:

db.collectionName.update(where, document || document[], options);
db.collectionName.updateOne(where, document, options);
db.collectionName.updateMany(where, document[], options);

To delete documents in a collection you can use the remove method

db.collectionName.remove(where, options);

Import and export

To import data to mongodb you can user two different commands mongorestore and mongoimport.

mongorestore restores data exported with indexes and metadata, the restored data should be a result of mongodump

mongorestore -d database folderName
mongorestore -d database -collection collection file.bson
mongorestore -h remoteServer -d database -u user -p password folderName

mongoimport creates or updates documents from json, csv or tsv files, a collection should be specified, no indexes and no metadata should be imported

mongoimport -d database -c collectionName file.json
mongoimport -h remoteServer -d database -c collection -u user -p password file.json

To export data from mongodb you can use mongodump and mongoexport. mongodump will export all the data in the database including indexes and metadata

mongodump -d database -o outputDir
mongodump -d database -c collection -o outputDir

mongoexport exports only data from a collection

mongoexport -d database -c collectionName file.json
mongoexport -h remoteServer -d database -c collection -u user -p password file.json

Indexes

Get indexes

db.collectionName.getIndexes();
db.movieDetails.getIndexes();

Simple index

db.collectionName.createIndex(keys, options);
db.movieDetails.createIndex({rated:1}, {sparse:1});

Compund index

db.movieDetails.createIndex({"awards.nominations":1});

Multikey index

db.movieDetails.createIndex({rated:1, metacritic: 1});

Text index:

db.movies.createIndex( { title: "text" } );
db.movieDetails.createIndex({title:"text", plot:"text", actors:"text"}, {weights: {title:10, actors:5}, name:"textIndex"})

db.movieDetails.find({$text:{$search:"Leo"}}, {score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}}).limit(5).pretty();

2d Spatial index

db.collection.createIndex({location:"2d"});
db.collection.find({location:{$near:[50, 50]}})

Delete index

db.collectionName.dropIndex(indexName);

Covered queries

db.movieDetails.explain("executionStats").find({rated:'PG',metacritic:'71'}, {_id:0});

mongo-basics's People

Contributors

acasasitexico avatar alcasas avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.