Giter Site home page Giter Site logo

mongodb_exercise-02's Introduction

MongoDB_Exercise-02

**1.Create a Database called customers.** ... test> use customers switched to db customers ...

2. Create a collection called customerdetails. ... db.createCollection("customerdetails") { ok: 1 } ...

3. Insert all documents into the collection named customerdetails. ... db.customerdetails.insertMany([{"name":"John","age":"25","gender":"Male","city":"New York"},{"name":"Emily","age":"25","gender":"Female","city":"London"},{"name":"Daniel","age":"22","gender":"Male","city":"Sydney"},{"name":"Sophia","age":"28","gender":"Female","city":"Paris"},{"name":"William","age":"26","gender":"Male","city":"Chicago"},{"name":"Olivia","age":"23","gender":"Female","city":"Los Angeles"},{"name":"Benjamin","age":"27","gender":"Male","city":"Toronto"},{"name":"Mila","age":"30","gender":"Female","city":"Berlin"},{"name":"James","age":"30","gender":"Male","city":"Tokyo"}]) { acknowledged: true, insertedIds: { '0': ObjectId("654cb07aa71a47ec4d1ae7d7"), '1': ObjectId("654cb07aa71a47ec4d1ae7d8"), '2': ObjectId("654cb07aa71a47ec4d1ae7d9"), '3': ObjectId("654cb07aa71a47ec4d1ae7da"), '4': ObjectId("654cb07aa71a47ec4d1ae7db"), '5': ObjectId("654cb07aa71a47ec4d1ae7dc"), '6': ObjectId("654cb07aa71a47ec4d1ae7dd"), '7': ObjectId("654cb07aa71a47ec4d1ae7de"), '8': ObjectId("654cb07aa71a47ec4d1ae7df") } }

... 4. Retrieve all documents from the collection and sort the results by the โ€œageโ€ field in ascending order. ... db.customerdetails.find().sort({age:1 }).pretty() [ { _id: ObjectId("654cb07aa71a47ec4d1ae7d9"), name: 'Daniel', age: '22', gender: 'Male', city: 'Sydney' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7dc"), name: 'Olivia', age: '23', gender: 'Female', city: 'Los Angeles' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7d7"), name: 'John', age: '25', gender: 'Male', city: 'New York' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7d8"), name: 'Emily', age: '25', gender: 'Female', city: 'London' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7db"), name: 'William', age: '26', gender: 'Male', city: 'Chicago' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7dd"), name: 'Benjamin', age: '27', gender: 'Male', city: 'Toronto' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7da"), name: 'Sophia', age: '28', gender: 'Female', city: 'Paris' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7de"), name: 'Mila', age: '30', gender: 'Female', city: 'Berlin' }, { _id: ObjectId("654cb07aa71a47ec4d1ae7df"), name: 'James', age: '30', gender: 'Male', city: 'Tokyo' } ]

... 5. Count the number of females.

... db.customerdetails.countDocuments({"gender":"Female"}) 4

... 6.Insert one document into the customerdetails collection.

... db.customerdetails.insertOne({"name":"Piranavan","age":"23","gender":"Male","city":"Jaffna"}) { acknowledged: true, insertedId: ObjectId("654cb70ca71a47ec4d1ae7e0") } ... 7. Update city=SriLanka to John. ... db.customerdetails.update({"name": "John"},{ $set: {"city": "Srilanka"}}) DeprecationWarning: Collection.update() is deprecated. Use updateOne, updateMany, or bulkWrite. { acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 } ...

8. Remove the customer from Tokyo. ... db.customerdetails.remove({ "city":"Tokyo" }) DeprecationWarning: Collection.remove() is deprecated. Use deleteOne, deleteMany, findOneAndDelete, or bulkWrite. { acknowledged: true, deletedCount: 1 } ... 9. Find customers not from Los Angeles. ... db.customerdetails.find({"city": {$ne: "Los Angeles" } }) [ { _id: ObjectId("654ca6a8f41d0bb8c74d2fb1"), name: 'John', age: '25', gender: 'Male', city: 'Srilanka' }, { _id: ObjectId("654ca6a8f41d0bb8c74d2fb2"), name: 'Emily', age: '22', gender: 'Female', city: 'London' }, { _id: ObjectId("654ca6a8f41d0bb8c74d2fb3"), name: 'Daniel', age: '28', gender: 'Male', city: 'Sydney' }, { _id: ObjectId("654ca6a8f41d0bb8c74d2fb4"), name: 'Sophia', age: '24', gender: 'Female', city: 'Paris' }, { _id: ObjectId("654ca6a8f41d0bb8c74d2fb5"), name: 'William', age: '26', gender: 'Male', city: 'Chicago' }, { _id: ObjectId("654ca6a8f41d0bb8c74d2fb7"), name: 'Benjamin', age: '27', gender: 'Male', city: 'Toronto' }, { _id: ObjectId("654ca6a8f41d0bb8c74d2fb8"), name: 'Mila', age: '29', gender: 'Female', city: 'Berlin' }, { _id: ObjectId("654cb000f41d0bb8c74d2fba"), name: 'Sajee', age: '24', gender: 'Male', city: 'Toronto' } ] ... 10.Find the customers who are older than age 25. ...

... 11.Retrieve the males who are less than 25. 12.Update Francis age to 35, if Francis is not available upsert. 13.Retrieve males who are younger than 30 and older than25. 14.Find a customer who is lesser than or equal to 23. 15.Remove the customer from Tokyo.

mongodb_exercise-02's People

Contributors

ragukumaran-piratheep 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.