Giter Club home page Giter Club logo

mongodb's Introduction

MongoDB

Table of Contents
  1. Insert many documents in a collection
  2. Shearch by field
  3. Search between dates
  4. Update a document by _id
  5. Search by field that match regex
  6. Search by fields that match regex - OR
  7. Search by fields that match regex - AND
  8. Delete document by _id
  9. Unset a document field that match

Insert many documents in a collection

db.movies.insertMany([{title:'Fight Club',writer: 'Chuck Palahniuk',year: 1999,actors: ['Brad Pitt', 'Edward Norton']},{title: 'Pulp Fiction',writer: 'Quentin Tarantino',year: 1994,actors: ['John Travolta', 'Uma Thurman']},{title: 'Inglourious Basterds',writer: 'Quentin Tarantino',year: 2009,actors: ['Brad Pitt', 'Diane Kruger', 'Eli Roth']},{title:'The Hobbit: An Unexpected Journey',writer: 'J.R.R. Tolkien',year: 2012,franchise:'The Hobbit'},{title: 'The Hobbit: The Desolation of Smaug',writer: 'J.R.R. Tolkien',year: 2013,franchise: 'The Hobbit'},{title: 'The Hobbit: The Battle of the Five Armies',writer: 'J.R.R. Tolkien',year: 2014,franchise: 'The Hobbit',synopsis: 'Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness.'},{title: 'Pee-wee s Big Adventure',year: 1985},{title: 'Avatar',year: 2009}])

Shearch by field

db.movies.find({writer:'Quentin Tarantino'})
db.movies.find({actors:'Brad Pitt'})
db.movies.find({franchise:'The Hobbit'})

Search between dates

db.movies.find({year:{$gte:1900, $lte:1999}})
db.movies.find({year:{$gte:2000, $lte:2010}})

Update a document by _id

db.movies.updateOne({_id: ObjectId("64b7f90c4a448846c65d6fd3")},{$set:{synopsis: 'A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with spirited group of dwarves to reclaim their montain home-and the gold within it- from the dragon Smaug.'}})
db.movies.updateOne({_id: ObjectId("64b7f90c4a448846c65d6fd4")},{$set:{synopsis: 'The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring.'}})

Search by field that match regex

db.movies.find({title: {$regex:"Hobbit"}})
db.movies.find({synopsis: {$regex:"Gandalf"}})

Search by fields that match regex - OR

db.movies.find({ "$or": [{"synopsis": {"$regex": "dwarves"} }, {"synopsis": {"$regex": "hobbit" }}]})

Search by fields that match regex - AND

db.movies.find({ "$and": [{"synopsis": {"$regex": "gold"} }, {"synopsis": {"$regex": "dragon" }}]})
db.movies.find({ "$and": [{"synopsis": {"$regex": "Bilbo"} }, {"synopsis":  {$not: {"$regex": "Gandalf" }}}]})

Delete document by _id

db.movies.deleteOne({_id: ObjectId("64b7f90c4a448846c65d6fd7")})

Unset a document field that match

db.movies.updateOne({title: 'Pee-wee s Big Adventure'},{ $unset: {title:''}})

(back to top)

mongodb's People

Contributors

adrianap00 avatar

Stargazers

Giancarmine avatar  avatar georgeKutsia avatar

Watchers

 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.