Population fails on single line arguments and wrong subs

20 views
Skip to first unread message

Davide Mannone

unread,
Mar 25, 2014, 6:21:50 AM3/25/14
to mongoo...@googlegroups.com
Hy everybody, hy Aaron

I'm trying to use the population mechanism by using the definition rules: 

AnySchema = new Schema({
  "collectionref": {type: [Number], ref: "collectionname", ...other such validate or defaults...},  // i'm using only Number ids 
                                                                                                                                     // and all ids of all collections begin always form 0 
                                                                                                                                     // and are unique only inside their own collection
  "otherrefs": {type: [Number], ref: "othercollectionnames", ...},
  "anysubdoc": {type: [anyotherschema], ...other such validate or defaults...}, 
  "anyotherproperies": ...such as also _id:Number; 
});

...and any of the referred collections like:

CollectionSchema = new Schama({"_id": Number, ...and any other property});  // many of them could have also other refs inside 
                                                                                                                 // also circular ones 
CollectionModel = model("collectionname", collectionschema);

...as soon I use any of the population mechanism like:

.find({}).populate("collectionref otherrefs ...").exec();

..or..

.find({}).populate({path: "collectionref", model:"collectioname"}).exec(); // with or without array version 
                                                                                                  // and any other pattern like the before or the next...

..or..

.find({}).populate("collectionref").populate("otherrefs").populate(...).exec();

I always got this kind of results:

1) some ref ar populated with objects/documents of other refs! Say we for this example: some of the collectionref are populated with otherrefs documents (and in a random manner, I suppose)!
2) not all ref are populated: some of them are left as numbers! I've noticed that every time the document with _id=0 is left without any populations.


...but...

if I write, let say, a matrioska code like this:

.find({}, cb(e,r){
  r.foreach(doc){  // semplified just as example...
    CollectionModel.populate(doc, "collectionref", cb(e, r2) {
       OtherModel.populate(doc,"otherrefs", cb(e,r3) {
          AnyOtherOrTheSameModel.populate(doc, "anypath.anylevel.anycollectionref", cb(e,r4) {
             return r;  // when all'elements are traversed   
          });
       });
    });
  });
});

In this way the final document r has all refs populated correctly and no one of them is left unchanged!

There is no problem to use the last version of populating documents, but what what happens? 
Why are the more compact versions not working?
What could this different behaviour depends from?

Davide Mannone

unread,
Mar 25, 2014, 6:51:13 AM3/25/14
to mongoo...@googlegroups.com
As integration, any of the space delimited refnames combination work fine too:

    CollectionModel.populate(doc, "collectionref othersamecollenctionref anypath.anylevel.anysamecollectionref", cb(e, r2) {
Reply all
Reply to author
Forward
0 new messages