简体   繁体   中英

Meteor and Mongo DB

I have two questions. 1) I have these two document in MongoDB calender and calendarios_slaves. calender has a subdocument with array of calendarios_slaves ObjectID. I have tried several queries but all to no result, How cxan I pull the ObjectID's of all the calendarios_slaves in the calender document.?

this is the calender document:

 {
    "_id" : ObjectId("577a09d3e9ac22d62a20ab01"),
    "status_visualizacion" : "visible",
    "status_tipo" : "Pintura",
    "createdAt" : ISODate("2016-07-04T07:01:39.018Z"),
    "usuarios_admin" : [ 
        ObjectId("5773976c201bb491f499c180"), 
        ObjectId("577a03db9da98306f624c3d9"), 
        ObjectId("577a041d9da98306f624c3da"), 
        ObjectId("577a07b7e9ac22d62a20aae9"), 
        ObjectId("577a07c6e9ac22d62a20aaea"), 
        "Ys6fiychXcSfCgWox"
    ],
    "grupo_usuarios" : [ 
        ObjectId("5773976c201bb491f499c180"), 
        ObjectId("577a03db9da98306f624c3d9"), 
        ObjectId("577a041d9da98306f624c3da"), 
        ObjectId("577a07b7e9ac22d62a20aae9"), 
        ObjectId("577a07c6e9ac22d62a20aaea")
    ],
    "calendario_slaves" : [ 
        ObjectId("577b6a0114b9512e1e3f4c10"), 
        ObjectId("577b6a1d14b9512e1e3f4c11"), 
        ObjectId("577b6a2414b9512e1e3f4c12")
    ]
}

2) Is there anyway of pulling out nodes from a query in javascript in meteor. I used a queries to get all whole document in meteor (Client) javascript but couldn't get the nodes even though I was able to do that in the html using the #each loop..

 Meteor.subscribe("calenders_user_visible");
    Template.calendarios_visibles.helpers({
    ls_calenderios_visibles: function(){
    var result = Calender.find({status_visualizacion: "visible"});
    return result;
    }
});





<template name= "calendarios_visibles">

Calendarios visible!

<ul>
     {{#each ls_calenderios_visibles}}
<li class = "calendarios_slave">  Calendarios slaves: {{calendario_slaves}} </li>
</ul>
{{/each }}
</template>

Result en Browser:

Calendarios visible!

Pintura visible
    Calendarios slaves: ObjectID("577b6a0114b9512e1e3f4c10"),ObjectID("577b6a1d14b9512e1e3f4c11"),ObjectID("577b6a2414b9512e1e3f4c12")
vehiculo visible
    Calendarios slaves: ObjectID("577b6a0114b9512e1e3f4c10"),ObjectID("577b6a1d14b9512e1e3f4c11"),ObjectID("577b6a2414b9512e1e3f4c12")
montaje visible
    Calendarios slaves: ObjectID("577b6a0114b9512e1e3f4c10"),ObjectID("577b6a1d14b9512e1e3f4c11"),ObjectID("577b6a2414b9512e1e3f4c12")

Is there any way of getting these ObjectIds in Javascript immediately after getting the queries result?? I am only interested in these ObjectId values to make other queries not to display them in the browser. Could any one with more experience help me out please? Thanks

For 1st :

There are two way if you are want to get a result for single or more than one calender doc.

For single doc :

   var calender = calenderCollection.findOne('ID');
   var result= [];
   if(calender.calendario_slaves.map){
       result = calender.calendario_slaves.map(function(data){
                    return data;
                })
   }
  return data;

For multiple doc :

   var calenders = calenderCollection.find({query}).fetch();
   var result= [];
   for ( var i = 0; i < calenders.length; i++ ) {
       for ( var j = 0; j < calenders[i].calendario_slaves; j++ ) {
           result.push(calenders[i].calendario_slaves[j]);
       }
   }
   return result;

For 2nd :

<ul>
     {{#each ls_calenderios_visibles}}
          {{#each calendario_slaves}}
          <li class = "calendarios_slave">  
              Calendarios slaves: {{this}} // You can pass this into another helper to perform some operation.
          </li>
          {{/each}}
     {{/each }}
</ul>

@note: your question 2 is not well clear for me, Please let me know if you need something else for question 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM