Giter Club home page Giter Club logo

mongodb_house_price_example's Introduction

House Price Example

Simple CRUD and aggregation example on a house price mongo database. It is using the mongo shell client.

Importing localhost

Run the command (you must have a mongo server running localhost):

$ mongorestore -d homeSales <HomeSalesDirectoryLocation>

It will create the database homeSales containing the data.

Running example

Run the mongo db client:

$ mongo
> use homeSales;

Then, for each example you can copy and paste the following:

  1. For creating example:
db.homeSales.insertOne(
{
      "amount" : 9000,
      "date" : ISODate("1996-09-19T00:00:00Z"),
      "address" : {
              "nameOrNumber" : 9,
              "street" : "NORFOLK PARK COTTAGES",
              "town" : "MAIDENHEAD",
              "county" : "WINDSOR AND MAIDENHEAD",
              "postcode" : "SL6 7DR"
      }
});

Get the insertedId field.

  1. For reading example:
db.homeSales.find(
{
     "_id": ObjectId("insertedId")
});
  1. For updating example:
db.homeSales.updateOne(
{
   "_id": ObjectId("insertedId")
},
{
   $set:
        {
          "amount": 5000
        }
}
);
  1. For deleting example:
db.homeSales.deleteOne(
{
   "_id": ObjectId("insertedId")
}
);
  1. For aggregation example 1:
db.homeSales.aggregate([
{
  $match: {
				amount: { 
        				$gte:3000000
        }
  }
}, 
{
  $lookup: {
				from: "postcodes", 
        localField: "address.postcode",
        foreignField: "postcode",
    		as: "postcode_docs"
  }
}
]);
  1. For aggregation example 2:
db.homeSales.aggregate([
  {
    $group: 
    {
      _id: {$year: "$date"},
      higestPrice: {$sum: "$amount"},
      lowestPrice: {$min: "$amount"},
      averagePrice: {$avg: "$amount"},
      priceStdDev: {$stdDevPop: "$amount"}
    }
  },
  {
    $sort: {_id: 1}
  }
]);

Reference

Dataset: MongoDB Example

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.