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

Update $pull an object from [Object]? #284

Open
rtcwp07 opened this issue Nov 24, 2015 · 10 comments
Open

Update $pull an object from [Object]? #284

rtcwp07 opened this issue Nov 24, 2015 · 10 comments

Comments

@rtcwp07
Copy link

rtcwp07 commented Nov 24, 2015

First of all I want to say thank you for providing these awesome packages, and I hope I'm asking this in the right place! Using Simple Schema and collection2, having trouble pulling one object from an array of objects. For a trivia game app, using $push to add new players is working fine, but for the life of me I can't figure out how to remove a particular player from the doc when they leave the game. Here's the schema:

GameRecords.attachSchema(
    new SimpleSchema({
    gamecode: {
      type: String
    },
    host: {
        type: String
    },
    players: {
      type: [Object],
        optional: true
    },
        "players.$.playerid": {
            type: String,
            defaultValue: null,
            optional: true
        },
        "players.$.playername": {
            type: String,
            defaultValue: null,
            optional: true
        },
        "players.$.score": {
            type: Number,
            defaultValue: null,
            optional: true
        },
    createdAt: {
        type: Date,
        autoValue: function() {
            if (this.isInsert) {
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            } else {
                this.unset();
            }
        }
    }
  })
);

I've tried every permutation of every schema variable and update method that I know is possible, including:

  1. blackbox: true
  2. getAutoValues: false
  3. validate: false
  4. $pull method, as:
GameRecords.update({_id: gamerecordId}, {$pull: {players: playerToPull}});

where playerToPull is the whole player object, right from the doc.
5) $set method, as:

GameRecords.update({_id: gamerecordId}, {$set: {players: newPlayers}});

where newPlayers is a splice of the original players array without the player object that I'm trying to remove.

Nothing I've tried has resulted in a successful update. Am I missing something obvious? Thanks for your help!

@jupiterkenji
Copy link

I'm experiencing the same behaviour, have you manage to get it resolved?

@rtcwp07
Copy link
Author

rtcwp07 commented May 10, 2016

No, unfortunately I never did resolve that issue. Instead I had to
redesign my application flow, which of course was a headache!

On Sat, May 7, 2016 at 1:00 AM, stewiegaribaldi [email protected]
wrote:

I'm experiencing the same behaviour, have you manage to get it resolved?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#284 (comment)

@jupiterkenji
Copy link

Thank you for your reply rtcwp07, in my case it was my subdocument has auto value, so I need to do

eventid: {
  type: String,
  autoValue: function() {
    if (!this.isSet && this.operator !== "$pull") { // require pull check
      return Random.id();
    }
  },
}

@rtcwp07
Copy link
Author

rtcwp07 commented May 12, 2016

Ah I see now! Great information, thanks.

On Wed, May 11, 2016 at 2:49 AM, stewiegaribaldi [email protected]
wrote:

Thank you for your reply rtcwp07, in my case it was my subdocument has
auto value, so I need to do

eventid: {
type: String,
autoValue: function() {
if (!this.isSet && this.operator !== "$pull") { // require pull check
return Random.id();
}
},
}


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#284 (comment)

@stefanve
Copy link

stefanve commented Oct 19, 2016

Have the same problem, can set and push but not pull
Error: After filtering out keys not in the schema, your modifier is now empty

This finds the element:
Orders.find({"handlingErrors.ticket.tempId": tempId}, {fields: {"handlingErrors.$.ticket": 1,_id: 0}}).fetch()

This updates it:
Orders.update({_id: orderId, "handlingErrors.ticket.tempId": tempId}, {$set: {"handlingErrors.$.ticket.contactData.email": newEmail }}

This does NOT pull it:
Orders.update({_id: orderId}, {$pull:{"handlingErrors.$.ticket.tempId": tempId}})

@stefanve
Copy link

Ooops, ignore my previous comment. I got the query wrong, this works:
Orders.update({_id: orderId}, {$pull:{"handlingErrors": {"ticket.tempId": tempId}}})

it needs to know what to pull...

@aldeed
Copy link
Collaborator

aldeed commented Feb 2, 2018

Closing old issues. Please comment if this is still an issue and should be reopened.

@aldeed aldeed closed this as completed Feb 2, 2018
@ChrisOelmueller
Copy link

@aldeed
I'm fairly certain this is still happening. Could you take a look at the following dummy sample and confirm that the expected outcome is "some things are present" while the actual result is empty things arrays all over the place?

I prepared it in Meteor (1.6.0) but probably similar results can be observed using just npm packages.

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';

s = new SimpleSchema({
    'things': {
        type: Array,
    },
    'things.$': {
        type: Object,
    },
    'things.$.name': {
        type: String,
    },
    'things.$.valid': {
        type: Boolean,
        optional: true,
        defaultValue: true,
    },
});

Hello = new Mongo.Collection('absolutelyFilledWithThings');
Hello.attachSchema(s);

Hello.insert({'things': [{
    'name': 'yep',
    'valid': true, // Note: The schema defaultValue for this property is also `true`
}, {
    'name': 'sure',
    'valid': true,
},
]});

Hello.update(
    {},
    {$pull: {'things': {'valid': 'cute-kitten'}}}, // Note: This shouldn't match anything
    {'multi': true}
); // Expected outcome: No changes, all inserted entries have a two-element `things` array
console.dir(Hello.find().fetch(), {'depth': 3});
// After the update is executed, however, all DB entries will have `things: []` (empty array!)

@aldeed
Copy link
Collaborator

aldeed commented Apr 11, 2018

Haven't had a chance to look yet, but I'll reopen for now

@aldeed aldeed reopened this Apr 11, 2018
@thumptech
Copy link

I can confirm I am having this problem and struggling to find a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants