Mongo DB find method confusion in Implicit AND - mongodb

I am new to mongodb I have started to learn basic syntax recently. I was trying operators with find method, and I got a confusing case while trying Implicit AND.
My Collection mathtable having 400 documents is as follows:
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b2") , "index" : 1 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b3") , "index" : 2 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b4") , "index" : 3 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b5") , "index" : 4 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b6") , "index" : 5 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b7") , "index" : 6 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b8") , "index" : 7 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4b9") , "index" : 8 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4ba") , "index" : 9 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bb") , "index" : 10 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bc") , "index" : 11 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bd") , "index" : 12 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4be") , "index" : 13 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4bf") , "index" : 14 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c0") , "index" : 15 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c1") , "index" : 16 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c2") , "index" : 17 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c3") , "index" : 18 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c4") , "index" : 19 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4c5") , "index" : 20 }
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4d1") , "index" : 1 }
..
..
{ "_id" : ObjectId("540efc2bd8af78d9b0f5d4z5") , "index" : 20 }
There are 400 rows in mathtable collection:
Value of index ranges from 1 to 20.
For each value of index there are 20 entries with different _id value.
I am trying below two operations and expecting same results, considering that they are both implicit AND cases.
Calculating even index values having a value greater than 5.
Using classic EXPLICIT AND ( results into 160 records ) :
db.mathtable.count({
$and: [
{ index: { $mod: [2,0] } },
{ index: { $gt: 5 } }
]
});
Using variable name only once ( results into 160 records ) :
db.mathtable.count({
index : { $mod : [2,0] , $gt:5 }
});
Using field name with every condition ( results into 300 records ):
db.mathtable.find({
index : { $mod : [2,0]} ,
index : {$gt:5}
});
Using field name with every condition, conditions in opposite order ( results into 200 records ):
db.mathtable.find({
index : {$gt:5} ,
index : { $mod : [2,0]}
});
There is no mention of implicit OR in mongoDB documentation( or at-least I did not find a direct reference like implicit AND ) .
I was expecting same count of records ( 160 ) in both cases. I am unable to understand why above codes are behaving differently.
Also, order of condition specification results into different number of results. As per observation, only the last condition specified in find was applied, when same field was specified multiple times. That is weird and incorrect.
NOTE: I am using Mongo-DB-2.6 and code is being executed on mongo shell that comes with the distribution.

Json or an associative array or a map does not contain duplicate keys:
db.mathtable.find({
index : { $mod : [2,0]} ,
index : {$gt:5}
});
The above will be considered equivalent to:
db.mathtable.find({
index : {$gt:5}
});
The first condition will be overwritten,
and the below,
db.mathtable.find({
index : {$gt:5} ,
index : { $mod : [2,0]}
});
will be equivalent to,
db.mathtable.find({
index : { $mod : [2,0]}
});
However in the first case,
db.mathtable.count({
$and: [
{ index: { $mod: [2,0] } },
{ index: { $gt: 5 } }
]
});
the $and takes two json documents as input and behaves as expected.
and in the second case, count takes a single document with no duplicate keys and behaves as expected.
db.mathtable.count({
index : { $mod : [2,0] , $gt:5 }
});
Hence the difference in the number of rows returned. Hope it is helpful.

Related

mongodb findOneAndUpdate only if a certain condition is met

Following is my mongo db entries.
my-mongo-set:PRIMARY> db.stat_collection.find({name : /s/})
{ "_id" : ObjectId("5aabf231a167b3808302b138"), "name" : "shankarmr", "email" : "abc#xyz", "rating" : 9901 }
{ "_id" : ObjectId("5aabf23da167b3808302b139"), "name" : "shankar", "email" : "abc1#xyz1", "rating" : 10011 }
{ "_id" : ObjectId("5aabf2b5a167b3808302b13a"), "name" : "shankar1", "email" : "abc2#xyz2", "rating" : 10 }
{ "_id" : ObjectId("5aabf2c2a167b3808302b13b"), "name" : "shankar2", "email" : "abc3#xyz3", "rating" : 100 }
Now i want to find an entry based on name but update a field only if a certain condition holds good.
I tried the following statement, but it gives me error at the second reference to $rating.
db.stat_collection.findOneAndUpdate({name: "shankar"}, {$set : {rating : {$cond : [ {$lt : [ "$rating", 100]}, 100, $rating]}}, $setOnInsert: fullObject}, {upsert : true} )
So in my case, it shouldnot update rating for the 2nd document as the rating is not less than 100. But for the third document, rating should be updated to 100.
How do i get it work?
$max is the operator you're looking for, try:
db.stat_collection.findOneAndUpdate( { name: "shankar1"}, { $max: { rating: 100 } }, { returnNewDocument: true } )
You'll either get old value (if is greater than 100) or modify a document and set 100
According to the documentation:
The $max operator updates the value of the field to a specified value if the specified value is greater than the current value of the field. The $max operator can compare values of different types, using the BSON comparison order.
You should put all conditions in the query part of the update:
db.stat_collections.findOneAndUpdate(
{ name: "Shankar", rating: { $lt: 100 } },
$set : { rating: 100 },
);
"If the name is Shankar and rating is less than 100, then set the rating to 100." is the above.

Positional operator and field limitation

In a find query projection, fields I specify after the positional operator are ignored and the whole document is always returned.
'myArray.$.myField' : 1 behave exactly like 'myArray.$' : 1
the positional operator selects the right document. But this document is quite big. I would like to project only 1 field from it.
Exemple:
db.getCollection('match').find({"participantsData.id" : 0001}, { 'participantsData.$.id': 1, })
here the response I have
{
"_id" : "myid",
"matchCreation" : 1463916465614,
"participantsData" : [
{
"id" : 0001,
"plenty" : "of",
"other" : "fields",
"and" : "subdocuments..."
}
]
}
This is what I want
{
"_id" : "myid",
"matchCreation" : 1463916465614,
"participantsData" : [
{
"id" : 0001
}
]
}
Is it possible with mongo?
Yes it can be done in mongo
Please try the below query
db.getCollection('match').find(
{"participantsData.id" : 0001},
{"participantsData.id": 1, "matchCreation": 1 })
This will give you the below result
{
"_id" : "myid",
"matchCreation" : 1463916465614,
"participantsData" : [
{
"id" : 1
}
]
}

MongoDB Remove All Rows with a Certain Field

How can I remove all rows/records/entries that have a certain property set in MongoDB? For example, how can I remove all the rows that have an x field without removing the last row, which has no x field?
{ "_id" : ObjectId("53907a0adf55a0a97263b36d"), "x" : 21 }
{ "_id" : ObjectId("53907a0adf55a0a97263b36e"), "x" : 22 }
{ "_id" : ObjectId("53907a0adf55a0a97263b36f"), "x" : 23 }
{ "_id" : ObjectId("53907a0adf55a0a97263b370"), "x" : 24 }
{ "_id" : ObjectId("53907a16df55a0a97263b372"), "name" : "Bob" }
I tried this, but it removed everything:
db.testData.remove({}, {x:""})
http://docs.mongodb.org/manual/reference/operator/query/exists/
db.testData.remove({x: {$exists: true}})

assertion exception in mongo mapreduce

I have a collection that stores search query logs. It's two main attributes are user_id and search_query. user_id is null for a logged out user. I am trying to run a mapreduce job to find out the count and terms per user.
var map = function(){
if(this.user_id !== null){
emit(this.user_id, this.search_query);
}
}
var reduce = function(id, queries){
return Array.sum(queries + ",");
}
db.searchhistories.mapReduce(map,
reduce,
{
query: { "time" : {
$gte : ISODate("2013-10-26T14:40:00.000Z"),
$lt : ISODate("2013-10-26T14:45:00.000Z")
}
},
out : "mr2"
}
)
throws the following exception
Wed Nov 27 06:00:07 uncaught exception: map reduce failed:{
"errmsg" : "exception: assertion src/mongo/db/commands/mr.cpp:760",
"code" : 0,
"ok" : 0
}
I looked at mr.cpp L#760 but could not gather any vital information. What could be causing this?
My Collection has values like
> db.searchhistories.find()
{ "_id" : ObjectId("5247a9e03815ef4a2a005d8b"), "results" : 82883, "response_time" : 0.86, "time" : ISODate("2013-09-29T04:17:36.768Z"), "type" : 0, "user_id" : null, "search_query" : "awareness campaign" }
{ "_id" : ObjectId("5247a9e0606c791838005cba"), "results" : 39545, "response_time" : 0.369, "time" : ISODate("2013-09-29T04:17:36.794Z"), "type" : 0, "user_id" : 34225174, "search_query" : "eficaz eficiencia efectividad" }
Looking at the docs I could see that this is not possible in the slave. It will work perfectly fine in the master though. If you still want to use the slave then you have to use the following syntax.
db.searchhistories.mapReduce(map,
reduce,
{
query: { "time" : {
$gte : ISODate("2013-10-26T14:40:00.000Z"),
$lt : ISODate("2013-10-26T14:45:00.000Z")
}
},
out : { inline : 1 }
}
)
** Ensure that the output document size does not exceed 16MB limit while using inline function.

Mongodb Map/Reduce - Multiple Group By

I am trying to run a map/reduce function in mongodb where I group by 3 different fields contained in objects in my collection. I can get the map/reduce function to run, but all the emitted fields run together in the output collection. I'm not sure this is normal or not, but outputting the data for analysis takes more work to clean up. Is there a way to separate them, then use mongoexport?
Let me show you what I mean:
The fields I am trying to group by are the day, user ID (or uid) and destination.
I run these functions:
map = function() {
day = (this.created_at.getFullYear() + "-" + (this.created_at.getMonth()+1) + "-" + this.created_at.getDate());
emit({day: day, uid: this.uid, destination: this.destination}, {count:1});
}
/* Reduce Function */
reduce = function(key, values) {
var count = 0;
values.forEach(function(v) {
count += v['count'];
}
);
return {count: count};
}
/* Output Function */
db.events.mapReduce(map, reduce, {query: {destination: {$ne:null}}, out: "TMP"});
The output looks like this:
{ "_id" : { "day" : "2012-4-9", "uid" : "1234456", "destination" : "Home" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "2345678", "destination" : "Home" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "3456789", "destination" : "Login" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "4567890", "destination" : "Contact" }, "value" : { "count" : 1 } }
{ "_id" : { "day" : "2012-4-9", "uid" : "5678901", "destination" : "Help" }, "value" : { "count" : 1 } }
When I attempt to use mongoexport, I can not separate day, uid, or destination by columns because the map combines the fields together.
What I would like to have would look like this:
{ { "day" : "2012-4-9" }, { "uid" : "1234456" }, { "destination" : "Home"}, { "count" : 1 } }
Is this even possible?
As an aside - I was able to make the output work by applying sed to the file and cleaning up the CSV. More work, but it worked. It would be ideal if I could get it out of mongodb in the correct format.
MapReduce only returns documents of the form {_id:some_id, value:some_value}
see: How to change the structure of MongoDB's map-reduce results?