question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem applying $unset and $pull operations on nested entities

See original GitHub issue

Describe the bug We have queries doing various field operations on embedded objects in our system. Until now we have been using Morphia 1.5 without any issues. But we are currently migrating our system to Morphia 2.2.9 and encountered some problems, mainly with the $unset and $pull operators where they are not translated to their full paths (e.g. “parent.field”).

To Reproduce Lets say we have following Morphia query:

Query<CWOrg> query = ds.find(Entity.class).filter(eq("_id", someId));
query
	.update(
		UpdateOperators.unset("subscriptionDetails.syncAttempts"),
		UpdateOperators.set("subscriptionDetails.lastSyncDate", syncDate))
	.execute(new UpdateOptions().multi(true));

Morphia will translate the full path for the $set operator but only use the mapped field name for the $unset operator (found similar issues with $pull). So logging the native Mongo query results in below update. This means that only the $set operator applies as the $unset has incorrect path.

[
  {
    "q": {"_id": {"$oid": "637b48c5735ce247d5759634"}
	"u": {
	  "$unset": {"syncAttempts": ""}, 
	  "$set": {"subscriptionDetails.lastSyncDate": {"$date": "2022-11-21T09:45:41.514Z"}}
	}, 
	"multi": true
  }
]

Expected behavior We would have expected that all operators are applied using their complete dot-notated path. But maybe we are misunderstanding something with the new APIs.

** Please complete the following information: **

  • Server Version: 4.2
  • Driver Version: 4.7
  • Morphia Version: 2.2.9

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
evanchoolycommented, Nov 22, 2022

OK. Confirmed and fixed both failures. After several attempts, I finally found a way to directly those in bulk and in various combos. It’s not pretty but gets the job done. I’ll try to push 2.2.10 tomorrow.

2reactions
evanchoolycommented, Nov 21, 2022

I will try to take a look at this tonight/tomorrow. I was on the verge of push 2.2.10 and this would be good to get in before then.

Read more comments on GitHub >

github_iconTop Results From Across the Web

removing object from nested array of objects mongodb
1. $unset removes document properties. · This is true. I definitely wanted to remove the entire object from the array, but I sure...
Read more >
5 Common Hibernate Mistakes That Cause Dozens of ...
When you now remove a Book entity, Hibernate cascades the operation to the associated Review entities. 1. 2. Book b = em.find(Book. class ......
Read more >
$pull — MongoDB Manual
The $pull operator treats each element as a top-level object. The query is applied to each element. The expression does not need to...
Read more >
Using Push & Pull Operators in MongoDB - BMC Software
Push and Pull are the two operations used to add and remove elements from arrays in MongoDB documents. Pushing and pulling elements are...
Read more >
Efficient Querying - EF Core - Microsoft Learn
Performance guide for efficient querying using Entity Framework Core. ... entire tables are typically sources of serious performance issues.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found