pymongo bson.code pass in variable - mongodb

I'm using pymongo's map_reduce framework for extracting some data out from mongodb. This is how my data look like:
{
"_id" : ObjectId("566f570e3816dc2fe631db4f"),
"property_id" : 5594.0000000000000000,
"reservation_id" : "2544430.1",
"updated" : ISODate("2015-12-15T02:04:33.000Z"),
"offer_list" : {
"68799" : {
"pitched" : "no",
"accepted" : "no"
},
"68801" : {
"pitched" : "no",
"accepted" : "no"
}
},
"status" : "awarded",
"comments" : "",
"agent_id" : 1.0000000000000000,
"created" : ISODate("2015-12-14T23:55:52.000Z")
}
I will need to get all record counts for each agent after some date using pymongo.
The mapper function would look like:
mapper = Code("""function(){
if(this.created>start_date){
emit(this.agent_id, 1);
})
in which start_date would be a variable passed in from python code.
By reading the document I think the scope should be set but I could not find any documentation on how to do that. I knew nothing about javascript. Can anyone help?
Thanks in advance!

The problem was fixed using the query keyword.
collection.map_reduce(mapper, reducer, "myresults", query={"created":{"$gte":start_date, "$lt":end_date}})

Related

MONGODB - cast type of every object in array of objects

I have MongoDB Collection where some documents have arrays of objects. One of the fields of this objects is timestamp.
The problem is that historically some of timestamp values are Strings (e.g. '2018-02-25T13:33:56.675000') or Date and some of them are Double (e.g. 1528108521726.26).
I have to convert all of them to Double.
I've built the query to get all the documents with the problematic type:
db.getCollection('Cases').find({sent_messages: {$elemMatch:{timestamp: {$type:[2, 9]}}}})
And I also know how to convert Date-string to double using JS:
new Date("2018-02-18T06:39:20.797Z").getTime()
> 1518935960797
But I can't build the proper query to perform the update.
Here is an example of such a document:
{
"_id" : ObjectId("6c88f656532aab00050dc023"),
"created_at" : ISODate("2018-05-18T03:43:18.986Z"),
"updated_at" : ISODate("2018-05-18T06:39:20.798Z"),
"sent_messages" : [
{
"timestamp" : ISODate("2018-02-18T06:39:20.797Z"),
"text" : "Hey",
"sender" : "me"
}
],
"status" : 1
}
After the update it should be:
{
"_id" : ObjectId("6c88f656532aab00050dc023"),
"created_at" : ISODate("2018-05-18T03:43:18.986Z"),
"updated_at" : ISODate("2018-05-18T06:39:20.798Z"),
"sent_messages" : [
{
"timestamp" : 1518935960797.00,
"text" : "Hey",
"sender" : "me"
}
],
"status" : 1
}
As per your question, you are trying to fetch the record first.
db.getCollection('Cases').find({sent_messages: {$elemMatch:{timestamp: {$type:[2, 9]}}}})
Then convert date in JS:
new Date("2018-02-18T06:39:20.797Z").getTime()
And then this is an update query:
db.getCollection('Cases').updateOne({_id:ObjectId("6c88f656532aab00050dc023")}, { $set: { "sent_messages.$.timestamp" : "218392712937.0" }})
And if you want to update all records then you should write some forEach mechanism. I think you have already this implemented.
Hope this may help you.
Finally I just do it with JS code that can be run in mongo console:
db.getCollection('Cases').find({sent_messages: {$elemMatch:{timestamp: {$type:[2, 9]}}}}).forEach(function(doc) {
print('=================');
print(JSON.stringify(doc));
doc.sent_messages.forEach(function(msg){
var dbl = new Date(msg.timestamp).getTime();
print(dbl);
msg.timestamp = dbl;
});
print(JSON.stringify(doc))
db.Cases.save(doc);
} )
Thanks all for your help!

Get all objects where a property matches a value nested in array

I have the following data table:
{
"_id" : ObjectId("value"),
"owner" : "testuser",
"date" : ISODate("2017-03-16T12:45:03.386Z"),
"location" : "thuis",
"venue" : "bijna thuis",
"description" : "fghgfh",
"completed" : false,
"winnerName" : null,
"subscriptions" : [],
"interactions" : [
{
"_id" : ObjectId("objectid"),
"owner" : "testuser",
"type" : "guess",
"date" : ISODate("2017-03-06T12:13:10.049Z"),
"answer" : false,
"message" : "test 1"
},
{
"_id" : ObjectId("objectid"),
"owner" : "testuser",
"type" : "guess",
"date" : ISODate("2017-03-06T12:13:10.049Z"),
"answer" : false,
"message" : "test 2"
}
],
"__v" : 0,
"active" : true
}
The above is just one game object. Which means we got several of this objects in our database.
I am trying to get only the interactions where the owner == "testuser".
The problem is that I cannot seem to figure out the best way to do this.
In my code I got 2 objects ( Game & Interaction) where Game has an array of interactions.
Is there someway I can still do this using the mongocsharpdriver.
Thanks in advance for all the help.
Hope it work for you Thanks :-)
collection.Find(x => x.owner == "testuser").ToList(); //where collection is MongoDB collection
Thanks for all the suggestions and sorry for the late response. But I find a way to solve it like this:
var filter = Builders<Game>.Filter.ElemMatch("interactions",
Builders<Interaction>.Filter.Eq("owner", owner));
var interactions = await MongoCollection.Find(filter).ToListAsync();
return interactions.SelectMany(item => item.Interactions).ToList();
This will it will return all the interactions that has a certain user as owner.
Hope I can help somebody out with this answer.

MongoDB .Net driver 2.0 Builders Filter (field to array comparison)

I need to get all usernames from "followingList.username" and compare with
posts' usernames, if there any match need to add that one to an array.
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is coming ..."
},
{
"_id" : ObjectId("554f2df2a388K4b425b89833"),
"username" : "gumbet",
"category" : "Fall",
"text" : "there are many trees"
}
I try to get result code block as below but couldn't figure it out.
var filter = Builders<Post>.Filter.AnyEq("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
Can you help me with this?
Thanks.
The logic is flipped, what you need is an $in query if I understand your use case correctly:
var filter = Builders<Post>.Filter.In("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
In your case, username is a simple field and you want to match against a list of candidates. AnyEq is used to check that, from an embedded list of complex objects, at least one matches a criterion. That still translates to a simple query in MongoDB, but requires to 'reach into' the object which needs a more complicated syntax.

mongodb set null in update

I have to interchange the values of a document.
var query = {"_id" : ObjectId("53e1c254382f891cc600076d")};
db.properties.find(query).forEach(function(prop){
printjson({"_id":prop._id, "val":prop.val, "ua":prop.ua});
db.properties.update(query, {$set:{ua: prop.val}},{$unset:{val:""}});
});
Before update operation, document looks like this:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : 9876541,
"ua" : null
}
And after the update it turns to:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : 9876541,
"ua" : 9876541
}
But I expect it as:
{
"_id" : ObjectId("53e1c254382f891cc600076d"),
"val" : null,
"ua" : 9876541
}
But its not working. also setting "val" null ({$set:{val:null}}) directly it deleted my entire document.
Setting undefined, It worked like a charm!!
db.properties.find(query).forEach(function(prop){
db.properties.update(query, {$set:{ua: prop.val, val:undefined}});
});
I know this is old, but your syntax for the update function is wrong. The update object should be one single object: db.properties.update(query, {{$set:{ua: prop.val}},{$unset:{val:""}}});

How to return specific field's value in mongodb

how can I return a specific value for a specific document in MongoDB? For example, I have a schema that looks like:
{
"_id" : "XfCZSje7GjynvMZu7",
"createdAt" : ISODate("2015-03-23T14:52:44.084Z"),
"services" : {
"password" : {
"bcrypt" : "$2a$10$tcb01VbDMVhH03mbRdKYL.79FPj/fFMP62BDpcvpoTfF3LPgjHJoq"
},
"resume" : {
"loginTokens" : [ ]
}
},
"emails" : {
"address" : "abc123#gmu.edu",
"verified" : true
},
"profile" : {
"companyName" : "comp1",
"flagged" : true,
"phoneNum" : "7778883333"
}}
I want to return and store the value for profile.flagged specifically for the document with _id : XfCZSje7GjynvMZu7. So far I have tried:
db.users.find({_id:'myfi3E4YTf9z6tdgS'},{admin:1})
and
db.users.find({_id: 'myfi3E4YTf9z6tdgS'}, {profile:admin});
I want the query to return true or false depending on the assigned value.
Can someone help? Thanks!
MongoDB queries always return document objects, not single values. So one way to do this is with shell code like:
var flagged =
db.users.findOne({_id: 'myfi3E4YTf9z6tdgS'}, {'profile.flagged': 1}).profile.flagged;
Note the use of findOne instead of find so that you're working with just a single doc instead of the cursor that you get with find.
The correct answer here is the method .distinct() (link here)
Use it like this:
db.users.find({_id:'myfi3E4YTf9z6tdgS'},{admin:1}).distinct('admin')
The result will be: 1 or 0