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.

Support for positional $ operator in Update

See original GitHub issue

It seems that mongo-java-server does not support the positional operator $ when an update is performed. I’m trying to run the following update, but it does not work in my tests (it works when I run the actual code):

Query query = new Query();
query.addCriteria(Criteria.where("myArray").elemMatch(
	Criteria.where("identifier").is("theId").and("state").is("theState")));

Update update = new Update()
    .set("myArray.$.identifier", newId)
    .set("myArray.$.state", newState);

this.mongoTemplate.updateMulti(query, update, MyEntity.class);

The error message is: The positional operator did not find the match needed from the query.

Would it be possible to fix this? It would be nice, if I am able to use my already exiting test in the future 😉

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bwaldvogelcommented, Jan 29, 2020

Ok, thanks for this hint. I’ll try to have a closer look soon

0reactions
emillyngecommented, Sep 14, 2022

I’m also seeing this problem.

I think the issue may be caused by DefaultQueryMatcher.checkMatchesElemValues not recording lastPosition when looping:

        Collection<Object> list = (Collection<Object>) values;
        for (Object value : list) {
            if (checkMatchesValue(queryValue, value, false)) {
                return true;
            }
        }

It does so in DefaultQueryMatcher.checkMatchesAnyValue(Object queryValue, Collection<?> values) which is why your test isn’t failing.
In order to provoke the issue, you need to use $elemMatch in the query.

Read more comments on GitHub >

github_iconTop Results From Across the Web

$ (update) — MongoDB Manual
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array....
Read more >
MongoDB - Positional Operator ($) - GeeksforGeeks
MongoDB provides different types of array update operators to update the values of the array fields in the documents and positional ...
Read more >
positional update operator with upsert - Stack Overflow
As D. SM said, you can use bulkWrite to do multiple operations in one go: const shopId = 0 collection.bulkWrite([ // Remove any...
Read more >
Support positional $ operator with aggregation pipeline for ...
I want to update the data-value of the data block with "block-id" : ObjectId("5fd3ba7eb0b91523b424e614") for the document with "_id" ...
Read more >
Added positional operator ($) support when updating ... - GitHub
Added positional operator ($) support when updating documents. #438. Open. HowlingGuineapig wants to merge 3 commits into louischatriot ...
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