Search This Blog

Thursday, 2 January 2020

MongoDB search operation - Searching in document and document with nested json objects

As you know MongoDB in no SQL database. MongoDB noSQL makes it faster than relational database. MongoDB based on documents. Document can be any valid json object. This json object can have nested structure. This article illustrates searching mechanism with MongoDB.

Lets have some proper json document 


{
  "name": "testUser",
  "deviceId": "android",
  "type": "contract",
  "address": "D14/150 London"
}
{
  "name": "testUser2",
  "deviceId": "android",
  "type": "contract",
  "address": "D14/150 London"
}
{
  "name": "testUser3",
  "deviceId": "android",
  "type": "contract",
  "address": "D14/150 London"
}

Insert documents like below using MongoDB shell


> db.analytics.insertOne({ "name": "testUser",   "deviceId": "android",   "type": "contract",   "address": "D14/150 London" });
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5e0dc9a3e6ebcea82c40836c")
}


Let's find documents where name is equal to testUser


> db.analytics.find({name:"testUser"}).pretty();
{
        "_id" : ObjectId("5e0dc9a3e6ebcea82c40836c"),
        "name" : "testUser",
        "deviceId" : "android",
        "type" : "contract",
        "address" : "D14/150 London"
}

Lets find documents where name is testUser and type is contract


> db.analytics.find({name:"testUser", type: "contract"}).pretty();
{
        "_id" : ObjectId("5e0dc9a3e6ebcea82c40836c"),
        "name" : "testUser",
        "deviceId" : "android",
        "type" : "contract",
        "address" : "D14/150 London"
}

Search in MongoDB in nested documents. Let's create some nested documents 



{
  "name": "newUser",
  "deviceId": "android",
  "type": "full",
  "address": "D14/150 London",
  "phone": {
    "home": 123404,
    "office": 485
  }
}
{
  "name": "newUser1",
  "deviceId": "android",
  "type": "full",
  "address": "D14/150 London",
  "phone": {
    "home": 000,
    "office": 485
  }
}

Here the result of query 


> db.analytics.find({"phone.home": 000}).pretty();
{
        "_id" : ObjectId("5e0dccaae6ebcea82c408371"),
        "name" : "newUser1",
        "deviceId" : "android",
        "type" : "full",
        "address" : "D14/150 London",
        "phone" : {
                "home" : 0,
                "office" : 485
        }
}

Thursday, 25 October 2018

Why Node.js® - several reasons of nodejs popularity?


Node.js®, known as Node is gaining attention of designer. Node has been proved as good option to write highly scalable network solutions. We recently choose Nodejs as our server language.

Asynchronous event driven JavaScript runtime 


Node is designed to build scalable network applications. Using nodejs many concurrent connection can be handle. On each connection the callback is fired, but if there is no work to be done, Node will sleep. This is opposite to common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use.

Node are free from worries of dead-locking the process


Users of Node are free from worries of dead-locking the process, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks.
Because nothing blocks, scalable systems are very reasonable to develop in Node.

Similarity with Ruby's Event machine and Python's twisted 


Node is similar in design and influenced by system like Ruby's Event machine and Python's twisted. Node took the event model a bit further. It presented an event loop as a runtime construct instead of as a library. In other systems there is always a blocking call to start the event-loop. This behavior is defined through callbacks at the beginning of a script and at the end starts a server through a blocking call like EventMachine::run().

In Node there is no such start-the-event-loop call. Node simply enters the event loop after executing the input script. Node exits the event loop when there are no more callbacks to perform. This behavior is like browser JavaScript — the event loop is hidden from the user


HTTP's the most important aspect of Nodejs


HTTP is a first class citizen in Node, designed with streaming and low latency in mind. This makes Node well suited for the foundation of a web library or framework.

Note - Just because Node is designed without threads, that doesn't mean one can't take
advantage of multiple core  in environment. Child processes can be spawned by using our child_process.fork() API, and are designed to be easy to communicate with. Built upon that same interface is the cluster module, which allows you to share sockets between processes
to enable load balancing over your cores. 
Android News and source code