How to add element to Mongo DB - mongodb

I am a newcomer in mongoDB and this is trying to add an element to an array using push, when I try on the arrays progreso or data_pjs there is no problem I have the problem when I try to insert it on the array datos_progreso
/* 1 */ {
"_id" : ObjectId("5a2a99935887b3f20e1294c6"),
"__v" : 0,
"player" : “Player1”,
"gremio" : "27866",
"progreso" : [
{
"gPowerC" : "2,592,335",
"gPowerS" : "1,594,634",
"fecha" : ISODate("2017-12-07T17:00:21.000Z"),
"_id" : ObjectId("5a2a99935887b3f20e1294c7")
},
{
"gPowerC" : "5,592,335",
"gPowerS" : "5,594,634",
"fecha" : ISODate("2017-12-10T17:00:21.000Z"),
"_id" : ObjectId("5a2aa12cc24daa1417b35107")
}
],
"datos_pjs" : [
{
"name" : "R2-D2",
"_id" : ObjectId("5a2a99935887b3f20e1295cc"),
"datos_progreso" : [
{
"level" : "85",
"gear" : "XII",
"power" : "19,873",
"star" : "7",
"fecha" : ISODate("2017-12-07T17:00:21.000Z"),
"_id" : ObjectId("5a2a99935887b3f20e1295cd")
},
{
/****** HERE ******/
}
]
},
{
"name" : "Jyn Erso",
"_id" : ObjectId("5a2a99935887b3f20e1295ca"),
"datos_progreso" : [
{
"level" : "85",
"gear" : "XII",
"power" : "19,873",
"star" : "7",
"fecha" : ISODate("2017-12-07T17:00:21.000Z"),
"_id" : ObjectId("5a2a99935887b3f20e1295cb")
}
]
}
] }
I try to add the element using this command but it does not work, I would push the element in the data_progress matrix in the R2-D2 character and at the same time push REY element to data_pjs, REY push if that works for me. Can the two operations be performed at the same time?
db.getCollection('pruebas').update (
{ "_id" : ObjectId("5a2a99935887b3f20e1294c6") },
{
$push: {
"datos_pjs" : {
"name" : "R2-D2",
"_id" : ObjectId("5a2a99935887b3f20e1295cc"),
"datos_progreso" : {
"level" : "87",
"gear" : "XII",
"power" : "9,873",
"star" : "5",
"fecha" : ISODate("2017-12-07T17:00:21.000Z"),
"_id" : ObjectId()
}
},
"datos_pjs" : {
"name" : "Rey",
"_id" : ObjectId(),
"datos_progreso" : {
"level" : "85",
"gear" : "XII",
"power" : "9,873",
"star" : "5",
"fecha" : ISODate("2017-12-07T17:00:21.000Z"),
"_id" : ObjectId()
}
}
}
}
)
can anybody help me?

Related

Update query on in the collection by "_id"

{
"_id" : "tenant/data/EMAIL/ENGLISH",
"tenantId" : "tenant2",
"channelType" : "EMAIL",
"template" : [
{
"_id" : "1",
"templateName" : "abc",
"effectiveStartDate" : ISODate("2017-01-01T12:00:00.000Z"),
"modifiedDate" : ISODate("2017-06-02T22:08:55.782Z"),
"active" : false
}
]
}
I need to update the "templateName" : "xyz" on the basis of "_id" : "tenant/data/EMAIL/ENGLISH"
I have tried these queries but got no success
db.getCollection('data').updateOne({"_id": "tenant/data/EMAIL/ENGLISH"},
{$set : { "template.$.templateName" : "XYZ"}}); 
db.getCollection('data').updateOne({"_id": "tenant/data/EMAIL/ENGLISH"},
{$set : { "template.templateName" : "XYZ"}}); 
Any help will be appreciated.
I have used positional-all operator to update the array.
Here is the query:
db.sample.update(
{
"_id": "tenant/data/EMAIL/ENGLISH"
},
{
$set:{
"template.$[].templateName":"XYZ"
}
}
)
Output
{
"_id" : "tenant/data/EMAIL/ENGLISH",
"tenantId" : "tenant2",
"channelType" : "EMAIL",
"template" : [
{
"_id" : "1",
"templateName" : "XYZ",
"effectiveStartDate" : ISODate("2017-01-01T12:00:00Z"),
"modifiedDate" : ISODate("2017-06-02T22:08:55.782Z"),
"active" : false
}
]
}
hope this will help :)

How to rewrite nested object in model while update

I have a problem with update object nested in array ("companyBases"), because update scripts overwrites my nested object, i have the following model:
{
"_id" : ObjectId("5d6504541be1e64145c20c66"),
"margin" : 10,
"defaultDeprication" : 10,
"companyBases" : [
{
"_id" : ObjectId("5d6504541be1e64145c20c64"),
"name" : "Tech Parking 2",
"street" : "Traktat Ojca",
"postalCode" : "30-856",
"city" : "Cracow",
"location" : {
"lng" : 50.036017,
"lat" : 20.086752
},
"__v" : 0
},
{
"_id" : ObjectId("5d6504541be1e64145c20c65"),
"name" : "Tech Parking 3",
"street" : "ul.Bieżanowska 258B",
"postalCode" : "30-856",
"city" : "Cracow",
"location" : {
"lng" : 50.01744,
"lat" : 20.033522
},
"__v" : 0
}
],
}
I'am executing update query:
db.companies.updateOne(
{
_id: ObjectId("5d6504541be1e64145c20c66"),
"companyBases._id": ObjectId("5d6504541be1e64145c20c64")
},
{
$set: {
"companyBases.$": {
"street" : "ul.Małapolska 123"
}
}
}
)
But it overwrites my nested object and now it looking like this:
{
"_id" : ObjectId("5d6504541be1e64145c20c66"),
"margin" : 10,
"defaultDeprication" : 10,
"companyBases" : [
{
"street" : "ul.Małapolska 123"
},
{
"_id" : ObjectId("5d6504541be1e64145c20c65"),
"name" : "Tech Parking 3",
"street" : "ul.Bieżanowska 258B",
"postalCode" : "30-856",
"city" : "Cracow",
"location" : {
"lng" : 50.01744,
"lat" : 20.033522
},
"__v" : 0
}
],
}
I would like to rewrite all field from nested object and update fields that i choose in update query.
It should looks like this (without overwriting whole object):
{
"_id" : ObjectId("5d6504541be1e64145c20c66"),
"margin" : 10,
"defaultDeprication" : 10,
"companyBases" : [
{
"_id" : ObjectId("5d6504541be1e64145c20c64"),
"name" : "Tech Parking 2",
"street" : "ul.Małapolska 123",
"postalCode" : "30-856",
"city" : "Cracow",
"location" : {
"lng" : 50.036017,
"lat" : 20.086752
},
"__v" : 0
},
{
"_id" : ObjectId("5d6504541be1e64145c20c65"),
"name" : "Tech Parking 3",
"street" : "ul.Bieżanowska 258B",
"postalCode" : "30-856",
"city" : "Cracow",
"location" : {
"lng" : 50.01744,
"lat" : 20.033522
},
"__v" : 0
}
],
}
You should use arrayFilter in the update operation, as described here:
positinal filter for arrays
db.companies.updateOne(
{
_id: ObjectId("5d6504541be1e64145c20c66"),
},
{
$set: {
"companyBases.$[element].street: "ul.Małapolska 123"
}
},
{
arrayFilters: [ {"element._id": ObjectId("5d6504541be1e64145c20c64")} ]
}
)

Mongodb update nested array by id

I have the following document and want to update state
Document ID: ObjectId("5a4e5a448b70d50e34d204a5")
Target ID: ObjectId("5a4e5a438b70d50e34d203ea")
I have no idea how to update the state to e.g. 4
{
"_id" : ObjectId("5a4e5a448b70d50e34d204a5"),
"name" : "Wirtschaftsdienst",
"date" : ISODate("2012-10-07T00:00:00.000Z"),
"comment" : null,
"tasks" : [
{
"name" : "Speisen und Getränke",
"sections" : [
{
"start" : 46800,
"end" : 72000,
"entirely" : true,
"assistants" : [
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203ea")
},
"state" : 3
},
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203f4")
},
"state" : 3
}
]
}
]
}
]
}
Use positional operator $[] along with arrayFilters to get your job done!
Try this query:
db.collection.update(
{"_id" : ObjectId("5a4e5a448b70d50e34d204a5")},
{$set: {"tasks.$[].sections.$[].assistants.$[element].state":4}},
{arrayFilters: [ {"element.assistant":{"_id" :
ObjectId("5a4e5a438b70d50e34d203ea")} }
], multi:true}
)
And the output is:
/* 1 */
{
"_id" : ObjectId("5a4e5a448b70d50e34d204a5"),
"name" : "Wirtschaftsdienst",
"date" : ISODate("2012-10-07T00:00:00.000Z"),
"comment" : null,
"tasks" : [
{
"name" : "Speisen und Getränke",
"sections" : [
{
"start" : 46800,
"end" : 72000,
"entirely" : true,
"assistants" : [
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203ea")
},
"state" : 4.0
},
{
"assistant" : {
"_id" : ObjectId("5a4e5a438b70d50e34d203f4")
},
"state" : 3.0
}
]
}
]
}
]
}

Compound GeoSpatial Index in MongoDB is not working as intended

the collection nodesWays has the following indexes:
> db.nodesWays.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "h595.nodesWays"
},
{
"v" : 1,
"key" : {
"amenity" : 1,
"geo" : "2dsphere"
},
"name" : "amenity_1_geo_2dsphere",
"ns" : "h595.nodesWays",
"2dsphereIndexVersion" : 2
},
{
"v" : 1,
"key" : {
"geo" : "2dsphere"
},
"name" : "geo_2dsphere",
"ns" : "h595.nodesWays",
"2dsphereIndexVersion" : 2
}
]
Now the following two queries should return the same result, but they don't.
I want the nearest 10 restaurants to the specified point.
The first query is working how it should be, the second is not working like intended.
The only difference between these two queries is that the first one uses the geo_2dsphere-Index
and the second query the amenity_1_geo_2dsphere-Index.
> db.nodesWays.find(
{
geo:{
$nearSphere:{
$geometry:{
type: "Point", coordinates: [9.7399777,52.3715156]
}
}
}, "amenity":"restaurant",
name: {$exists: true}
}, {id:1, name:1}).hint( "geo_2dsphere" ).limit(10)
{ "_id" : ObjectId("53884860e552e471be2b7192"), "id" : "321256694", "name" : "Masa" }
{ "_id" : ObjectId("53884860e552e471be2b7495"), "id" : "323101271", "name" : "Bavarium" }
{ "_id" : ObjectId("53884862e552e471be2ba605"), "id" : "442496282", "name" : "Naxos" }
{ "_id" : ObjectId("53884860e552e471be2b7488"), "id" : "323101189", "name" : "Block House" }
{ "_id" : ObjectId("53884878e552e471be2d1a41"), "id" : "2453236451", "name" : "Maestro" }
{ "_id" : ObjectId("53884870e552e471be2c8aab"), "id" : "1992166428", "name" : "Weinstube Leonardo Ristorante" }
{ "_id" : ObjectId("53884869e552e471be2c168b"), "id" : "1440320284", "name" : "Altdeutsche küche" }
{ "_id" : ObjectId("53884861e552e471be2b88f7"), "id" : "353119010", "name" : "Mövenpick" }
{ "_id" : ObjectId("5388485de552e471be2b2c86"), "id" : "265546900", "name" : "Miles" }
{ "_id" : ObjectId("53884863e552e471be2bb5d3"), "id" : "532304135", "name" : "Globetrotter" }
> db.nodesWays.find(
{
geo:{
$nearSphere:{
$geometry:{
type: "Point", coordinates: [9.7399777,52.3715156]
}
}
}, "amenity":"restaurant",
name: {$exists: true}
}, {id:1, name:1}).hint( "amenity_1_geo_2dsphere" ).limit(10)
{ "_id" : ObjectId("53884875e552e471be2cf4a8"), "id" : "2110027373", "name" : "Schloßhof Salder" }
{ "_id" : ObjectId("5388485be552e471be2aff19"), "id" : "129985174", "name" : "Balkan Paradies" }
{ "_id" : ObjectId("5388485be552e471be2afeb4"), "id" : "129951134", "name" : "Asia Dragon" }
{ "_id" : ObjectId("53884863e552e471be2ba811"), "id" : "450130115", "name" : "Kings Palast" }
{ "_id" : ObjectId("53884863e552e471be2ba823"), "id" : "450130135", "name" : "Restaurant Montenegro" }
{ "_id" : ObjectId("53884877e552e471be2d053a"), "id" : "2298722569", "name" : "Pizzaria Da-Lucia" }
{ "_id" : ObjectId("53884869e552e471be2c152e"), "id" : "1420101752", "name" : "Napoli" }
{ "_id" : ObjectId("5388485be552e471be2b0028"), "id" : "136710095", "name" : "Europa" }
{ "_id" : ObjectId("53884862e552e471be2ba5bc"), "id" : "442136241", "name" : "Syrtaki" }
{ "_id" : ObjectId("53884863e552e471be2ba763"), "id" : "447972565", "name" : "Pamukkale" }
My goal with the second index is to:
select all restaurants
then use the nearSphere-Operator to sort them in regards to the distance from the specified point
Auf Wiedersehen
I think you should try to put the geolocation first in the index.

What is reason that on mapreduce sometimes the mapper generates more documents than the original data in mongodb?

I am performing a state-wise population count and getting extra documents with the original output. To check the reason i found that mappers would generate intermediate data a lot of more than the original data in mongodb . How can i resolve this ? The total count of document in source collection is 29468.
Sample from the Dataset:
{ "city" : "SPLENDORA", "loc" : [ -95.199308, 30.232609 ], "pop" : 11287, "state" : "TX", "_id" : "77372" }
{ "city" : "SPRING", "loc" : [ -95.377329, 30.053241 ], "pop" : 33118, "state" : "TX", "_id" : "77373" }
{ "city" : "TOMBALL", "loc" : [ -95.62006, 30.073923 ], "pop" : 19801, "state" : "TX", "_id" : "77375" }
{ "city" : "WILLIS", "loc" : [ -95.497583, 30.432025 ], "pop" : 9988, "state" : "TX", "_id" : "77378" }
{ "city" : "KLEIN", "loc" : [ -95.528481, 30.023377 ], "pop" : 35275, "state" : "TX", "_id" : "77379" }
{ "city" : "CONROE", "loc" : [ -95.492392, 30.225725 ], "pop" : 1635, "state" : "TX", "_id" : "77384" }
map function:
var m=function(){ emit(this.city,this.pop);}
reduce function:
var r=function(c,p){ return p;}
MR output to a new collection :
{ "_id" : "81080", "value" : 172 }
{ "_id" : "81250", "value" : 467 }
{ "_id" : "82057", "value" : 60 }
{ "_id" : "95411", "value" : 133 }
{ "_id" : "95414", "value" : 226 }
{ "_id" : "95440", "value" : 2876 }
{ "_id" : "95455", "value" : 843 }
{ "_id" : "95467", "value" : 328 }
{ "_id" : "95489", "value" : 358 }
{ "_id" : "95495", "value" : 367 }
{ "_id" : "98791", "value" : 5345 }
{ "_id" : "PLEASANT GROVE", "value" : [ 8458, 15703, 80, 772,
{ "_id" : "POINTBLANK", "value" : 2911 }
{ "_id" : "PORTER", "value" : [ 13541, 19024, 985, 425, 2705 ]
{ "_id" : "SHEPHERD", "value" : [ 9604, 17397, 2078 ] }
{ "_id" : "SPLENDORA", "value" : 11287 }
{ "_id" : "SPRING", "value" : [ 33118, 8379, 21805, 8540 ] }
{ "_id" : "TOMBALL", "value" : 19801 }
{ "_id" : "WILLIS", "value" : [ 9988, 2769, 2574 ] }
{ "_id" : "KLEIN", "value" : 35275 }
Your output isn't as expected because your reduce function is incorrect. The prototype for a reduce function is function(key,values) {...}, where values is an array associated with the key.
Your reduce function is returning the values array rather than reducing it.
To sum up the values for a given key, your reduce() function should look like:
var r=function(key, values) {
return Array.sum(values);
}
If you want to calculate population by state, your map() function is also incorrect: you should be emitting the state & population instead of city & population:
var m=function() {
emit(this.state,this.pop);
}
Putting that together, your output should end up looking like:
{
"_id" : "AK",
"value" : 550043
},
{
"_id" : "AL",
"value" : 4040587
},
{
"_id" : "AR",
"value" : 2350725
}
...
The MongoDB manual has further details on writing and testing your reduce function:
Requirements for the reduce function
Troubleshooting the reduce function