Asked  7 Months ago    Answers:  5   Viewed   43 times

When I try to change any part of the data returned by a Mongoose Query it has no effect.

I was trying to figure this out for about 2 hours yesterday, with all kinds of _.clone()s, using temporary storage variables, etc. Finally, just when I though I was going crazy, I found a solution. So I figured somebody in the future (fyuuuture!) might have the save issue.

Survey.findById(req.params.id, function(err, data){
    var len = data.survey_questions.length;
    var counter = 0;

    _.each(data.survey_questions, function(sq){
        Question.findById(sq.question, function(err, q){
            sq.question = q; //has no effect

            if(++counter == len) {
                res.send(data);
            }
        });
    });
});

 Answers

48

For cases like this where you want a plain JS object instead of a full model instance, you can call lean() on the query chain like so:

Survey.findById(req.params.id).lean().exec(function(err, data){
    var len = data.survey_questions.length;
    var counter = 0;

    _.each(data.survey_questions, function(sq){
        Question.findById(sq.question, function(err, q){
            sq.question = q;

            if(++counter == len) {
                res.send(data);
            }
        });
    });
});

This way data is already a plain JS object you can manipulate as you need to.

Tuesday, June 1, 2021
 
DiglettPotato
answered 7 Months ago
14

Yes, the main difference is the ability to do computations before you save or as a reaction to information that comes up while you're building your new model. The most common example would be making sure the model is valid before trying to save it. Some other examples might be creating any missing relations before saving, values that need to be calculated on the fly based on other attributes, and models that need to exist but could potentially never be saved to the database (aborted transactions).

So as a basic example of some of the things you could do:

var instance = new MyModel();

// Validating
assert(!instance.errors.length);

// Attributes dependent on other fields
instance.foo = (instance.bar) ? 'bar' : 'foo';

// Create missing associations
AuthorModel.find({ name: 'Johnny McAwesome' }, function (err, docs) {
  if (!docs.length) {
     // ... Create the missing object
  }
});

// Ditch the model on abort without hitting the database.
if(abort) {
  delete instance;
}

instance.save(function (err) {
  //
});
Sunday, August 1, 2021
 
giraffeslacks
answered 5 Months ago
82

To use a field named type in an embedded sub-document like storage, you have to use an explicit definition or Mongoose gets confused:

var elementSchema = new Schema({
  name: String,
  storage: {
    url: String,
    type: { type: String }
  }
});
Wednesday, August 4, 2021
 
Xavio
answered 4 Months ago
16

Have a look at $query->num_rows (<- clickable).

Monday, August 16, 2021
 
LoicTheAztec
answered 4 Months ago
70
List<customer> custList = GetAllCustomers();
            dataGridView1.Rows.Clear();

            foreach (Customer cust in custList)
            {
                //First add the row, cos this is how it works! Dont know why!
                DataGridViewRow R = dataGridView1.Rows[dataGridView1.Rows.Add()];
                //Then edit it
                R.Cells["Name"].Value = cust.Name;
                R.Cells["Address"].Value = cust.Address;
                R.Cells["Phone"].Value = cust.PhoneNo;
                //Customer Id is invisible but still usable, like,
                //when double clicked to show full details
                R.Tag = cust.IntCustomerId;
            }

http://aspdiary.blogspot.com/2011/04/adding-new-row-to-datagridview.html

Tuesday, August 17, 2021
 
Boas Enkler
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share