Pull an entry from an array via Meteor Pull an entry from an array via Meteor mongodb mongodb

Pull an entry from an array via Meteor


For a basic meteor application, I call "bunk" on this. If you create a brand new project and simply define the collection, then the $pull operator works as expected:

Console:

meteor create ticketscd ticketsmeteor run

Then open up a shell and insert your data:

meteor mongo> db.tickets.insert(data)   // exactly your data in the question

Then just produce some basic code and template:

tickers.js

Tickers = new Meteor.Collection("tickers");if (Meteor.isClient) {  Template.body.helpers({    "tickers": function() {      return Tickers.find({});    }  });}if (Meteor.isServer) {  Meteor.startup(function () {    // code to run on server at startup  });}

tickers.html

<head>  <title>tickers</title></head><body>  <h1>Welcome to Meteor!</h1>  <ul>    {{#each tickers}}      {{> ticker}}    {{/each}}  </ul></body><template name="ticker">  <li>    {{_id}}    <ul>      {{#each entries}}        {{> entry }}      {{/each}}    </ul>  </li></template><template name="entry">  <li>{{ id }} - {{text}}</li></template>

The application should be running fine, so in your browser console do the .update() (indented for reading):

Tickers.update(    { "_id": "ZcEvq9viGQ3uQ3QnT" },    { "$pull": { "entries": { "id": "fc29774dadd7b37ee0dc5e3e" } }})

And the item is removed from entries and the page is refreshed without the item. So all gone, just as expected.

Even adding the SimpleSchema and Collection2 packages, makes no difference here:

 meteor add aldeed:simple-schema meteor add aldeed:collection2

tickers.js

Tickers = new Meteor.Collection("tickers");TickerEntries = new SimpleSchema({  "id": {    type: String,    optional: true,    autoValue: function() {      if (!this.isSet) {        return new Mongo.Collection.ObjectID()._str      }    }  },  "text": {    type: String  }});Tickers.attachSchema(  new SimpleSchema({    entries: { type: [TickerEntries] }  }));if (Meteor.isClient) {  Template.body.helpers({    "tickers": function() {      return Tickers.find({});    }  });}if (Meteor.isServer) {  Meteor.startup(function () {    // code to run on server at startup  });}

Re-initialize the data and run the same command in the browser console and everything stays the same.

Check this or any typing mistakes in your own operations or other differences for a clue as to why this does not work for you.

I would suggest this strongly, as "starting fresh" like this shows the expected behavior, and if you are seeing different behavior then it is likely an issue with another plugin you have installed.

But generally, this works.


Just to mention, if someone else is looking for an answer.

If you are usein SimpleSchema, you have two options:the array field should be marked as optional

arr: { type:Array, optional:true}

Or use getAutoValues: false in update-query.

Coll.update({}, {$pull: {arr: ''}}, {getAutoValues: false});