1

The JSON coming from an external web API looks like this:

[{matches:
    [{
      "match_something":"123",
      "match_something_else":"Potato",
      "match_events":
      [{
          "event_id":"42",
          "event_desc":"redcard",
       },
       {
           "event_id":"1",
           ..
      }]
    },
    // more matches

So, match array with an event array within it for each match.

The relevant processing code looks like this:

        _.each(matches, function(match) {
            var results = new Results({
                _id: match.match_id,
                match_date: match.match_formatted_date,
                ft_score: match.match_ft_score,
                match_events:[]
            });
            events = match.match_events;
            _.each(events, function(event) {
               results.match_events.push({
                   _id:event.event_id,
                   match_id:event.event_match_id,
                   type:event.event_type,
                   team:event.event_team,
                   player:event.event_player,
               });
            });
            results_array.push(results);
        });
        return results_array

This is the schema for the model (shortened for brevity):

var resultsSchema = new db.mongoose.Schema({
        _id:Number,
        match_date:String,
        status:String,
        ...
        match_events:[{
            _id: Number,
            match_id: Number,
            type:String,
            ...
        }]
    });

And then, what I see from my database (mongo) once this completes is the following JSON (removed extra attributes for clarity):

[
{"_id":1931559, "ft_score":"[0-0]","__v":0,
    "match_events":   
        ["19315591","19315592","19315593","19315594"]},

Which just has me baffled. The ID's are correct, I checked against the server data. And the processing code is just creating an array of these ID's, instead of a JSON object for each event.

Shouldn't it be shown as:

..."match_events":
    [{"_id:" "19315591", ...}]
4
  • I am not familiar with this database, but isn't the _id reserved? I mean are you sure you can define an object with _id in a nested structure? It seems like it interprets that as an internal link, or something like that. I'd try it out without _id... The code seems to be okay, so this is a strange bug... Commented Sep 5, 2014 at 2:14
  • Yep the problem was with the schema declaration as pointed out in the other answer below. I did remove the _id though just in case that would cause issues in the future. Thanks Commented Sep 5, 2014 at 2:21
  • @inf3rno Mongoose will use whatever "type" you declare explicitly for _id in the schema, or implicitly adds a ObjectId where you do not define it. It will even try to "cast" arguments supplied as a string to the correct type where it can. Commented Sep 5, 2014 at 2:52
  • I am not sure whether this is good or bad. I mean it is hard to debug if there are no error messages... At least it should send a console.warn()... Commented Sep 5, 2014 at 2:59

1 Answer 1

3

Your schema definition is the problem here. Mongoose uses the "type" keyword to determine the datatype, so it thinks that "match_events" is an array of "String".

Declare like this instead:

var resultSchema = new Schema({
  _id: Number,
  status: String,
  match_events: [{
    _id: { type: Number },
    type: { type: String }
  }]
});

Or better yet like this:

var eventSchema = new Schema({
  _id: Number,
  type: String
});

var resultSchema = new Schema({
  _id: Number,
  status: String,
  match_events: [eventSchema]
});
Sign up to request clarification or add additional context in comments.

4 Comments

Worked like a charm straight away. Thank you for the extra effort with the second implementation. Also thanks again for the help Neil, appreciate it.
Nice, at least the reserved word part was true :-)
Ah actually! Just had a look and mongo assigned an _id to each internal structure when I had removed _id from the schema. So it looks like it's required :)
At least we know that too :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.