$addToSet nested nested object using pymongo - python

As part of a for-in loop in python using pymongo I want to add some nested documents/objects inside a linktype field which is to be within a links field:
neither the links field or linktype field exists before the first such entries are to be added.
What is the commands to do this ?
Here is an item before adding links:
item = {
"_id" : ObjectId("5067c26b9d595266e25e825a"),
"name": "a Name"
}
And after adding one link of type typeA:
toType = "typeA"
to_link = {"_id" : ObjectId("5067c26b9d595266e25e825b"), "property":"value"}
{
"_id" : ObjectId("5067c26b9d595266e25e825a"),
"name": "a Name",
"links" : {
"typeA":{
{"_id" : ObjectId("5067c26b9d595266e25e825b"), "property":"value"}
}
}
}
I have tried:
db.collection.update({"name":"a Name"},{{"links":{"$addToSet":{toType:to_link}}})
which doesnt work.
If I just use:
db.collection.update({"name":"a Name"},{ {"$addToSet":{toType:to_link}} )
that works but that is not what i want.

$addToSet is for adding to an array. To add a new property to an existing embedded object you need to use the $set operator and dot notation as:
db.collection.update({name: 'a name'}, {$set: {'links.' + toType: to_link}})

Related

How to know the last time a product has been updated MongoD?

I am working on a project where I want to filter by the products that hasn't been updated in 2 months or a determinated date.(that don't have a new item price in the last 2 months or any other date I want to)
I want to do the script in python.
All my db are json that follow this estructure:
And to access it i do mongo_client[db_name][coll_name] and then i normally use .find() or .aggregate()
{
"_id" : ObjectId("6188f511091533324af78fbf"),
"market" : "x",
"product" : "apple",
"item_price_history" : [
{
"item_price" : 219.0,
"date" : ISODate("2021-04-08T15:30:43.000Z")
},
{
"item_price" : 248.0,
"date" : ISODate("2021-04-22T08:02:28.000Z")
}
Do you have any idea of how can I do that? I use the lastest version of Python and Robo 3T-1.4
Thanks in advance
You can look at the data in the item_price_history to check when that field was last updated. But you don't seem to have a way to track when the other field were updated.
Going forward, you could try adding a pre-save hooks to store the last updated datetime if you're using an ODM like MongoEngine.
Refer pre_save method here.
import dateutil.parser
list_to_sort = {
"market" : "x",
"product" : "apple",
"item_price_history" : [
{
"item_price" : 219.0,
"date" : "2021-04-08T15:30:43.000Z"
},
{
"item_price" : 248.0,
"date" : "2021-04-22T08:02:28.000Z"
}]
}
candidates = list_to_sort.values()
for item in candidates:
if isinstance(item,list):
list_to_sort = item
def myfunc(item):
time = dateutil.parser.parse(item["date"])
return time.timestamp()
list_to_sort.sort(key=myfunc)
print(list_to_sort)
this will sort the list based on custom function myfunc

Delete a list item from an Embedded Document MongoEngine

I'm having trouble to find the way to delete a list item inside an Embedded document using mongo engine.
Document structure:
{
"_id" : "id",
"user" : ObjectId("xxxxxxx"),
"invoice" : false,
"coupon" : ObjectId("xxxxxxx"),
"date" : ISODate("2017-03-31T11:32:57.467Z"),
"orders" : [
{
"products" : [
ObjectId("xxxxxxx")
]
}
],
"shipping_address" : {
},
}
Using this structure, what I want to achieve, is to delete the id inside the products list inside the orders embedded document.
Any idea? any help would be really appreciated

Find in referenced/linked MongoDB document, using pymongo

I have 3 linked documents like:
{'_id':1, 'name':'abc', 'label':'actionA', 'prev':null}
{'_id':2, 'name':'pqr', 'label':'actionB', 'prev':ObjectId('1')}
{'_id':3, 'name':'xyz', 'label':'actionC', 'prev':ObjectId('2')}
Now I want to query a document whose 'name' is 'pqr' and also its previous/linked document should contains 'label' as 'actionA'.
All I want is it should find 'name' and check whether previous liked doc is available, if so then check its previous doc should have 'label' which I want.
It will be preferable if using some 1 line command something like:
db.collection.find({'$and'[{'name':'pqr'},{'prev': <gotoprev>({'label':'actionA'})}]})
you can achieve this using aggregation
MongoDB 3.4 Solution
take advantage of the $graphLookup operator:
db.collection.aggregate([
{
$match:{
"name":"pqr"
}
},
{
$graphLookup:{
from:"collection",
startWith:"$prev",
connectFromField:"prev",
connectToField:"_id",
as:"parent",
maxDepth:1,
restrictSearchWithMatch:{
label:"actionA"
}
}
}
])
Mongodb 3.2
filter out document where name != 'pqr' in a $match stage
link parent an child with $lookup
unwind the resulting array with $unwind
finally filter out document where parent.label != 'actionA'
here is the query:
db.collection.aggregate([
{
$match:{
"name":"pqr"
}
},
{
$lookup:{
from:"collection",
localField:"prev",
foreignField:"_id",
as:"prev"
}
},
{
$unwind:"$prev"
},
{
$match:{
"prev.label":"actionA"
}
}
])
You can denormalise and store prev_label also in the referenced document (NoSQL way).
{'_id':1, 'name':'abc', 'label':'actionA', 'prev':null}
{'_id':2, 'name':'pqr', 'label':'actionB', 'prev':ObjectId('1'),'prev_label': 'actionA'}
{'_id':3, 'name':'xyz', 'label':'actionC', 'prev':ObjectId('2'),'prev_label': 'actionB'}
Then you can use find query for the result
db.collection.find({'$and'[{'name':'pqr'},{'prev_label': 'actionA'}]})
If label in original document is changed you can keep them updated in referenced documents with an update query
db.collection.update({'prev': updatedDocumentId},{'$set': {'prev_label': newLabel}}, multi=True)

update nth document in a nested array document in mongo

I need to update a document in an array inside another document in Mongo DB.
{
"_id" : ObjectId("51cff693d342704b5047e6d8"),
"author" : "test",
"body" : "sdfkj dsfhk asdfjad ",
"comments" : [
{
"author" : "test",
"body" : "sdfkjdj\r\nasdjgkfdfj",
"email" : "test#tes.com"
},
{
"author" : "hola",
"body" : "sdfl\r\nhola \r\nwork here"
}
],
"date" : ISODate("2013-06-30T09:12:51.629Z"),
"permalink" : "mxwnnnqafl",
"tags" : [
"ab"
],
"title" : "cd"
}
If I try to update first document in comments array by below command, it works.
db.posts.update({'permalink':"cxzdzjkztkqraoqlgcru"},{'$inc': {"comments.0.num_likes": 1}})
But if I put the same in python code like below, I am getting Write error, that it can't traverse the element. I am not understanding what is missing!!
Can anyone help me out please.
post = self.posts.find_one({'permalink': permalink})
response = self.posts.update({'permalink': permalink},
{'$inc':"comments.comment_ordinal.num_likes": 1}})
WriteError: cannot use the part (comments of comments.comment_ordinal.num_likes) to traverse the element
comment_ordinal should be a substitution, not the index itself. You're treating it like an integer that can be mapped to an ordinal number. I mean you should do something like:
updated_field = "comments." + str(comment_ordinal) + ".num_likes"
response = self.posts.update({'permalink': permalink}, {'$inc': {updated_field: 1}})
Hope this helps.
You are doing it wrong you need to build your query dynamically and the best way to do that is using the str.format method.
response = self.posts.update_one(
{'permalink': permalink},
{'$inc': {"comments.{}.num_likes".format(comment_ordinal): 1}}
)
Also you should consider to use the update_one method for single update and update_many if you need to update multiple documents because update is deprecated.

Pull from a list in a dict using mongoengine

I have this Document in mongo engine:
class Mydoc(db.Document):
x = db.DictField()
item_number = IntField()
And I have this data into the Document
{
"_id" : ObjectId("55e360cce725070909af4953"),
"x" : {
"mongo" : [
{
"list" : "lista"
},
{
"list" : "listb"
}
],
"hello" : "world"
},
"item_number" : 1
}
Ok if I want to push to mongo list using mongoengine, i do this:
Mydoc.objects(item_number=1).update_one(push__x__mongo={"list" : "listc"})
That works pretty well, if a query the database again i get this
{
"_id" : ObjectId("55e360cce725070909af4953"),
"x" : {
"mongo" : [
{
"list" : "lista"
},
{
"list" : "listb"
},
{
"list" : "listc"
}
],
"hello" : "world"
},
"item_number" : 1
}
But When I try to pull from same list using pull in mongo engine:
Mydoc.objects(item_number=1).update_one(pull__x__mongo={'list': 'lista'})
I get this error:
mongoengine.errors.OperationError: Update failed (Cannot apply $pull
to a non-array value)
comparising the sentences:
Mydoc.objects(item_number=1).update_one(push__x__mongo={"list" : "listc"}) # Works
Mydoc.objects(item_number=1).update_one(pull__x__mongo={"list" : "listc"}) # Error
How can I pull from this list?
I appreciate any help
I believe that the problem is that mongoengine doesn't know the structure of your x document. You declared it as DictField, so mongoengine thinks you are pulling from DictField not from ListField. Declare x as ListField and both queries should work just fine.
I suggest you should also create an issue for this:
https://github.com/MongoEngine/mongoengine/issues
As a workaround, you can use a raw query:
Mydoc.objects(item_number=1).update_one(__raw__={'$pull': {'x.mongo': {'list': 'listc'}}})

Categories

Resources