Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would Mongoose be saving the same document multiple times?
    primarykey
    data
    text
    <p>(Having discovered the answer, I've pared down my question to remove the less relevant bits, so that it will be more readable/useful to any future readers.)</p> <hr> <p>I'm testing out my schema/models in Mongoose to see if they work as expected. They do, mostly, except I've found that I sometimes end up with the same documents being saved multiple times.</p> <p>I'll walk through what I'm trying. First I load the model and confirm that the collection is empty:</p> <pre><code>var Artist = require('model-Artist'); Artist.find({}, function(err, docs) { docs.forEach(function(doc) { doc.remove(); }); // delete all docs to be safe }); Artist.count({}, console.log); // =&gt; null 0 </code></pre> <p>Alright, so the collection is empty. Then I create a new document using <code>new</code> with the mongoose model:</p> <pre><code>var nedRorem = new Artist(); nedRorem.names.push({ sort: 'Rorem, Ned', display: 'Ned Rorem', brief: 'Rorem', personal: true }); nedRorem.born = { year: 1923, month: 10, date: 23 }; nedRorem.places.push('USA'); </code></pre> <p>At this point I check to see how the doc looks:</p> <pre><code>&gt; nedRorem { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time), _id: 522a0694e9a7813c23000020, specialGenres: [], seeAlso: [], memberOf: [], places: [ 'USA' ], ensemble: false, names: [ { sort: 'Rorem, Ned', display: 'Ned Rorem', brief: 'Rorem', _id: 522a0694e9a7813c23000035, index: true, personal: true } ] } </code></pre> <p>Looks good. Note the _id. Then I save, and check the count:</p> <pre><code>nedRorem.save(); Artist.count({}, console.log); // =&gt; null 1 </code></pre> <p>Excellent! Let's begin creating another artist:</p> <pre><code>var catharineCrozier = new Artist(); catharineCrozier.names.push({ sort: 'Crozier, Catharine', display: 'Catharine Crozier', personal: true }); </code></pre> <p>Note that I haven't saved the new one yet. But how many Artists are there now?</p> <pre><code>Artist.count({}, console.log); // =&gt; null 3 </code></pre> <p>!!??? So who are they?</p> <pre><code>Artist.find({}, console.log); &gt; null [ { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time), _id: 522a08a7af52934c1200000a, __v: 0, specialGenres: [], seeAlso: [], memberOf: [], places: [ 'USA' ], ensemble: false, names: [ { sort: 'Rorem, Ned', display: 'Ned Rorem', brief: 'Rorem', _id: 522a08a7af52934c1200000b, index: true, personal: true } ] }, { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time), _id: 522a08a8af52934c1200000d, __v: 0, specialGenres: [], seeAlso: [], memberOf: [], places: [ 'USA' ], ensemble: false, names: [ { sort: 'Rorem, Ned', display: 'Ned Rorem', brief: 'Rorem', _id: 522a08a8af52934c1200000e, index: true, personal: true } ] }, { born: Mon Oct 22 1923 20:28:00 GMT-0400 (Eastern Daylight Time), _id: 522a08a8af52934c12000010, __v: 0, specialGenres: [], seeAlso: [], memberOf: [], places: [ 'USA' ], ensemble: false, names: [ { sort: 'Rorem, Ned', display: 'Ned Rorem', brief: 'Rorem', _id: 522a08a8af52934c12000011, index: true, personal: true } ] } ] </code></pre> <p>There are three documents of the first Artist, and they all have different ._id's from the original one I created.</p> <p>So here's where I define the Artist Schema:</p> <pre><code>var artistSchema = new mongoose.Schema({ names: [{ sort: String, display: String, brief: String, nonLatin: String, personal: { type: Boolean, default: false }, index: { type: Boolean, default: true } }], born: ucaDate(true), died: ucaDate(), ensemble: { type: Boolean, index: true, default: false }, places: { type: [ String ], index: true }, memberOf: [ { type: ObjectID, ref: 'Artist' } ], seeAlso: [ { type: ObjectID, ref: 'Artist' } ], specialGenres: [{ name: String, parent: String, classical: Boolean }] }); </code></pre> <p>What gives?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload