Pushing object into array using MongoDB syntax and SimpleSchema validation

The idea is to pop an object that looks like this into a named field likes

, which is an array:

{
  movieId: "VgtyvjVUAjf8ya",
  information: {
                 genre: "Action",
                 length: "160",
                 language: "English"
               }
}

      

I thought this would do it:

Meteor.users.update({_id: Meteor.userId()}, {$push: {likes: {movieId: movieId, information: informationObj}}})

      

But either this is wrong, or the check with SimpleSchema has some problems (she doesn't complain though) because all I get is an empty object in the array! And no, there is nothing wrong with the values ​​themselves, I checked.

The SimpleSchema for the field in question looks like this:

likes: {
            type: [Object],
            optional: true
        }

      

I've tried reading the documentation, but I'm not quite sure what happened. Somebody knows?

+3


source to share


1 answer


If you don't want to check for objects that fall into a property likes

, you can set blackbox

to true

in your schema, for example:

likes: {
    type: [Object],
    optional: true,
    blackbox: true
}

      

This will allow you to put whatever you want in an "as" object.



If you want to test objects of type "like", you will need to create additional schemas, for example:

var likeInfoSchema = new SimpleSchema({
    genre: {
        type: String
    },
    length: {
        type: String
    },
    language: {
        type: String
    }
});

var likeSchema = new SimpleSchema({
    movieId: {
        type: String
    },
    information: {
        type: likeInfoSchema
    }
});

Meteor.users.attachSchema(new SimpleSchema({
    // ...
    likes: {
        type: [likeSchema]
    }
}));

      

+7


source







All Articles