MongoDB code for Mapreduce,Aggregation and Index

[student@localhost ~]$ su
Password:
su: Authentication failure
[student@localhost ~]$ su
Password:
[root@localhost student]# systemctl start mongod
[root@localhost student]# ./mongo
bash: ./mongo: No such file or directory
[root@localhost student]# mongo
MongoDB shell version: 2.4.6
connecting to: test
> use spp
switched to db spp
> db.createCollection("fruits");
{ "ok" : 1 }
> db.fruits.insert({"name":"Apple","cost":200})
> db.fruits.insert({"name":"Apple","cost":200})
> db.fruits.insert({"name":"Apple","cost":300})
> db.fruits.mapReduce(function(){emit(this.name,this.price);}function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find();
Thu Jan  1 14:51:33.905 SyntaxError: Unexpected token function
> db.fruits.mapReduce(function(){emit(this.name,this.cost);}function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find();
Thu Jan  1 14:52:09.796 SyntaxError: Unexpected token function
> db.fruits.mapReduce(function(){emit(this.name,this.cost);},function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find();
{ "_id" : "Apple", "value" : 700 }

> show dbs
abs    0.203125GB
admin    (empty)
local    0.078125GB
pratiksha    0.203125GB
spp    0.203125GB
svh    0.203125GB


> use spp
switched to db spp


> show collections
MapReduce_F
fruits
system.indexes
> db.MapReduce_F.find();
{ "_id" : "Apple", "value" : 700 }
> db.fruits.mapReduce(function(){emit(this.name,1);},function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find();
{ "_id" : "Apple", "value" : 3 }
> use spp
switched to db spp
> db.fruits.insert({"name":"Banana","cost":60})
> db.fruits.insert({"name":"Banana","cost":60})
> db.fruits.mapReduce(function(){emit(this.name,1);},function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find();
{ "_id" : "Apple", "value" : 3 }
{ "_id" : "Banana", "value" : 2 }
> db.fruits.mapReduce(function(){emit(this.name,this.cost);},function(key,values){return Array.sum(values)},{out:"MapReduce_F"}).find();
{ "_id" : "Apple", "value" : 700 }
{ "_id" : "Banana", "value" : 120 }
>















> db.fruits.aggregate([{$group:{"_id":"$name","No of items":{$sum:1}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "No of items" : 2
        },
        {
            "_id" : "Apple",
            "No of items" : 3
        }
    ],
    "ok" : 1
}
> db.fruits.aggregate([{$group:{"_id":"$name","total":{$sum:$cost}}}])
Thu Jan  1 15:07:14.627 ReferenceError: $cost is not defined
> db.fruits.aggregate([{$group:{"_id":"$name","total":{$sum:"$cost"}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "total" : 120
        },
        {
            "_id" : "Apple",
            "total" : 700
        }
    ],
    "ok" : 1
}
> db.fruits.aggregate([{$group:{"_id":"$name","total":{$min:"$cost"}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "total" : 60
        },
        {
            "_id" : "Apple",
            "total" : 200
        }
    ],
    "ok" : 1
}
> db.fruits.aggregate([{$group:{"_id":"$name","total":{$max:"$cost"}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "total" : 60
        },
        {
            "_id" : "Apple",
            "total" : 300
        }
    ],
    "ok" : 1
}
> db.fruits.aggregate([{$group:{"_id":"$name","total":{$avg:"$cost"}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "total" : 60
        },
        {
            "_id" : "Apple",
            "total" : 233.33333333333334
        }
    ],
    "ok" : 1
}


> db.fruits.aggregate([{$group:{"_id":"$name","First":{$first:"$cost"}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "First" : 60
        },
        {
            "_id" : "Apple",
            "First" : 200
        }
    ],
    "ok" : 1
}


> db.fruits.aggregate([{$group:{"_id":"$name","Last":{$last:"$cost"}}}])
{
    "result" : [
        {
            "_id" : "Banana",
            "Last" : 60
        },
        {
            "_id" : "Apple",
            "Last" : 300
        }
    ],
    "ok" : 1
}








> db.fruits.createIndex({"name":1})
> db.fruits.createIndex({"name":1})

> db.fruits.createIndex({"cost":1})

> db.fruits.getIndexes();
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "spp.fruits",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "ns" : "spp.fruits",
        "name" : "name_1"
    },
    {
        "v" : 1,
        "key" : {
            "cost" : 1
        },
        "ns" : "spp.fruits",
        "name" : "cost_1"
    }
]
> db.fruits.dropIndex("cost_1");
{ "nIndexesWas" : 3, "ok" : 1 }
> db.fruits.getIndexes();
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "spp.fruits",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "name" : 1
        },
        "ns" : "spp.fruits",
        "name" : "name_1"
    }
]
>

















>

Popular posts from this blog

DDL DML DCL and TCL

Implementation of Calculator using lex and yacc

A Register Allocation algorithm that translates the given code into one with a fixed number of registers.