Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New upsert fails with $push and $each #1492

Closed
mcbain opened this issue Oct 12, 2013 · 4 comments
Closed

New upsert fails with $push and $each #1492

mcbain opened this issue Oct 12, 2013 · 4 comments

Comments

@mcbain
Copy link

@mcbain mcbain commented Oct 12, 2013

Collection.update works as expected, but new upsert not. It
simply pushes the whole element {"$each":["A","B","C"],"$slice":-3} to the elements array.

Tinytest.add("upsert - update handles $push with $each correctly", function (test) {
    var collection = new Meteor.Collection(Random.id());

    var id = collection.insert({name: 'jens'});

    var result = collection.update(id,
        {
            $push: {
                elements: {
                    $each: ['A', 'B', 'C'],
                    $slice: -3
                }}});

    test.equal(collection.findOne(id),
        {
            _id: id,
            name: 'jens',
            elements: ['A', 'B', 'C']
        }
    );
});

Tinytest.add("upsert - handles $push with $each correctly if doc not exists", function (test) {
    var collection = new Meteor.Collection(Random.id());

    var result = collection.upsert(
        {name: 'jens'},
        {
            $push: {
                elements: {
                    $each: ['A', 'B', 'C'],
                    $slice: -3
                }}});

    test.equal(collection.findOne(result.insertedId),
        {
            _id: result.insertedId,
            name: 'jens',
            elements: ['A', 'B', 'C']
        }
    );
});
@estark37
Copy link
Contributor

@estark37 estark37 commented Oct 12, 2013

Hi @mcbain. This actually appears to be a limitation of minimongo's implementation of modifiers, which the upsert implementation uses in some cases.

C = new Meteor.Collection("foo", { connection: null })
C.insert({"foo:"bar"})
C.update({foo:"bar"}, {$push: {bar: {$each: [1,2]}}})
C.findOne()
> {"_id":"gxFYoooNw8QjYhPhD","foo":"bar","bar":[{"$each":[1,2]}]}

I'll look into it to see if this can be easily fixed. Thanks for the report and the clear reproduction!

@mcbain
Copy link
Author

@mcbain mcbain commented Oct 12, 2013

The real problem is that happens also at server side, where {upsert:true} worked before.

@glasser
Copy link
Member

@glasser glasser commented Oct 15, 2013

Good point about Meteor not implementing the new $each/$sort/$slice Mongo $push syntax! I just implemented them: 3aca36a

Thanks for writing these tests, too! I'd like to include them in Meteor, though you'll have to accept our CLA first: https://contribute.meteor.com/

glasser added a commit that referenced this issue Oct 18, 2013
Test adapted by mcbain's test from #1492.
@glasser
Copy link
Member

@glasser glasser commented Oct 18, 2013

I included your tests as well. Thanks!

@glasser glasser closed this Oct 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants