Mongoose: Model.save()λŠ” ν¬ν•¨λœ 배열을 μ €μž₯ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2012λ…„ 11μ›” 10일  Β·  26μ½”λ©˜νŠΈ  Β·  좜처: Automattic/mongoose

λ‚΄ν¬λœ λ¬Έμ„œ 배열이 있으며, 이λ₯Ό λ³€κ²½ν•˜κ³  λͺ¨λΈμ—μ„œ .save()λ₯Ό μˆ˜ν–‰ν•˜λ©΄ λ‚΄ν¬λœ λ¬Έμ„œμ— λ³€κ²½ 사항이 μ €μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ‹€μŒμ€ λ‚΄ λͺ¨λΈκ³Ό μŠ€ν‚€λ§ˆμž…λ‹ˆλ‹€(간결함을 μœ„ν•΄ 일뢀 μƒλž΅).

var Votes = new mongoose.Schema({
    date: Date, 
    user_name: String,
    is_up: Boolean
});

var EventSchema = new mongoose.Schema({
  to_date: Date,
  from_date: Date,
  location: String,
  name: String,
  suggested_by: String,
  description: String,
  users: [EventUser],
  comments: [Comments],
  suggestions: [Event],
  votes: [Votes]
});

var Event = mongoose.model('Event', EventSchema);

event.save()κ°€ 호좜되기 μ „μ˜ λͺ¨λΈ:

{ 
     __v: 1,
    _id: 509e87e583ccbfa00e000004,
    description: 'Pfft',
    from_date: Sun Nov 11 2012 08:00:00 GMT-0500 (EST),
    location: 'Home',
    name: 'Whatever',
    suggested_by: 'No one',
    to_date: Sun Nov 11 2012 00:00:00 GMT-0500 (EST),
    votes: [],
    suggestions: 
     [ { users: [],
         comments: [],
         suggestions: [],
         votes: [],
         _id: 509e880883ccbfa00e000005,
         suggested_by: 'Some one',
         to_date: Sun Nov 11 2012 04:00:00 GMT-0500 (EST),
         from_date: Mon Nov 12 2012 00:00:00 GMT-0500 (EST),
         location: 'Home',
         name: 'Football',
         description: 'FOOTBALL!!' } ],
    comments: [],
    users: [] 
}

event.save() κ°€ 호좜되기 직전에 μ€‘μ²©λœ νˆ¬ν‘œκ°€ μžˆλŠ” λ™μΌν•œ κ°œμ²΄κ°€ ν˜ΈμΆœλ©λ‹ˆλ‹€.

{
   "__v":1,
   "_id":"509e87e583ccbfa00e000004",
   "description":"Pfft",
   "from_date":"2012-11-11T13:00:00.000Z",
   "location":"Home",
   "name":"Whatever",
   "suggested_by":"No one",
   "to_date":"2012-11-11T05:00:00.000Z",
   "votes":[ ],
   "suggestions":
      [ {
         "users":[],
         "comments":[ ],
         "suggestions":[ ],
         "votes":
            [{
               "is_up":true,
               "date":"2012-11-10T18:05:25.796Z",
               "user_name":"No one"
            }],
         "_id":"509e880883ccbfa00e000005",
         "suggested_by":"Some one",
         "to_date":"2012-11-11T09:00:00.000Z",
         "from_date":"2012-11-12T05:00:00.000Z",
         "location":"Home",
         "name":"Football",
         "description":"FOOTBALL!!"
      }],
   "comments":[],
   "users":[]
}

event.save()κ°€ 호좜되면 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•Šμ§€λ§Œ μ€‘μ²©λœ 이벀트 μŠ€ν‚€λ§ˆ λ‚΄λΆ€μ˜ μ€‘μ²©λœ νˆ¬ν‘œ μŠ€ν‚€λ§ˆλŠ” μ‹€μ œλ‘œ μ €μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ΅œμƒμœ„ 이벀트 κ°œμ²΄μ—μ„œ λ™μΌν•œ 전체 논리λ₯Ό μ‚¬μš©ν•˜μ—¬ νˆ¬ν‘œλ₯Ό μ €μž₯ν•˜λ©΄ μž‘λ™ν•©λ‹ˆλ‹€.

μ½”λ“œλ₯Ό μ‚΄νŽ΄λ³΄λ©΄ κ°„λ‹¨νžˆ .save()κ°€ μƒˆ 개체λ₯Ό μ €μž₯ν•˜κ³  이미 μ‘΄μž¬ν•˜λŠ” 개체λ₯Ό μ—…λ°μ΄νŠΈν•˜λŠ” 지름길이라고 κ°€μ •ν•©λ‹ˆλ‹€.

λ‚΄ 직감은 Model.prototype._deltaκ°€ λͺ¨λ“  μ€‘μ²©λœ 개체λ₯Ό μž‘μ„ 만큼 μΆ©λΆ„νžˆ κΉŠμ–΄μ§€μ§€ μ•ŠλŠ”λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. https://github.com/LearnBoost/mongoose/blob/master/lib/model.js#L529

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ¬Έμ œκ°€ 무엇인지 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. λͺ½κ΅¬μŠ€ FAQ에 λŒ€ν•œ 첫 번째 질문의 μ „ν˜•μ μΈ μ‚¬λ‘€μž…λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€. 제 κ²½μš°μ—λŠ” 이 λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€! :)

model.myArray[index] = anyValue

λœλ‹€

model.myArray.set(index, anyValue)

λͺ¨λ“  26 λŒ“κΈ€

문제λ₯Ό μž¬ν˜„ν•˜λŠ” 데 μ‚¬μš©ν•˜λŠ” μ½”λ“œλ„ ν¬ν•¨ν•˜μ‹­μ‹œμ˜€(μ €μž₯ν•˜κΈ° 전에 λ¬Έμ„œλ₯Ό μ‘°μž‘ν•˜λŠ” μœ„μΉ˜ λ“±).

λ¬Όλ‘ , 여기에 μ½”λ“œ 벽을 ν”Όν•˜κΈ° μœ„ν•΄ μš”μ μ— κ²Œμ‹œν–ˆμŠ΅λ‹ˆλ‹€. https://gist.github.com/4055392

λ‚˜λŠ” μ΅œμƒμœ„ μ΄λ²€νŠΈμ— νˆ¬ν‘œλ₯Ό μ €μž₯ν•˜λŠ” 것(μž‘λ™)κ³Ό μ€‘μ²©λœ μ΄λ²€νŠΈμ— νˆ¬ν‘œλ₯Ό μ €μž₯ν•˜λŠ” 것(μž‘λ™ν•˜μ§€ μ•ŠμŒ)을 λͺ¨λ‘ ν¬ν•¨ν–ˆμŠ΅λ‹ˆλ‹€. λ³΄μ‹œλ‹€μ‹œν”Ό νˆ¬ν‘œλ₯Ό μΆ”κ°€ν•˜κ±°λ‚˜ μ—…λ°μ΄νŠΈν•œ λ‹€μŒ λΉ„μŠ·ν•œ λ°©μ‹μœΌλ‘œ μ €μž₯ν•©λ‹ˆλ‹€.

suggestions μ‚¬μš©λœ EventSchema μ°Έμ‘°λ₯Ό μˆ˜μ •ν•΄ λ³΄μ„Έμš”.

// bad
var EventSchema = new mongoose.Schema({
  suggestions: [EventSchema] <== at this time, EventSchema is undefined which is interpreted as Mixed

// instead...

var EventSchema = new mongoose.Schema;
EventSchema.add({
  suggestions: [EventSchema] <== EventSchema exists
})


λ‹«κΈ°, 응닡이 μ—†μŠ΅λ‹ˆλ‹€. ν•„μš”ν•œ 경우 λ‹€μ‹œ μ—΄μ–΄μ£Όμ„Έμš”.

.save()κ°€ ν¬ν•¨λœ 객체λ₯Ό μ—…λ°μ΄νŠΈν–ˆμ§€λ§Œ μ‹€μ œλ‘œ mongodb에 μ €μž₯λ˜μ§€ μ•Šμ€ λ¬Έμ„œλ₯Ό λ°˜ν™˜ν•˜λŠ” λ™μΌν•œ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. λ„μ™€μ£Όμ„Έμš”.

@ Manojkumar91 이것을 μž¬ν˜„ ν•˜λŠ” μ½”λ“œλ₯Ό μ œκ³΅ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? 맀우 도움이 될 κ²ƒμž…λ‹ˆλ‹€ :)

λ‚˜λŠ” 이것을 κ²½ν—˜ν–ˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ° λ‚΄ λͺ¨λΈμ΄ μžˆμŠ΅λ‹ˆλ‹€.

/*jslint node:true */
"use strict";

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var measurement = new Schema({
    name: {type: String, required: true},
    instruction: {type: String, required: true}
});

var size = new Schema({
    name: {type: String, required: true},
    lengths: [String]
});

var chartSchema = new Schema({
    name: {type: String, required: true },
    diagram: {type: String, required: true },
    description: {type: String, required: true},
    totalMeasurements: {type: Number, required: true},
    totalSizes: {type: Number, required: true},
    measurements: [measurement],
    sizes: [size]
});

module.exports = mongoose.model('chart', chartSchema);

이것은 훨씬 더 큰 μ•±μ˜ μΌλΆ€μ΄λ―€λ‘œ κ΄€λ ¨ 컨트둀러 μž‘μ—… ν•„ν„°λ§Œ ν¬ν•¨ν•˜κ³  λ‚˜λ¨Έμ§€λŠ” 더 큰 의미둜 말할 수 μžˆμŠ΅λ‹ˆλ‹€. μ•„λž˜λŠ” ν•„ν„° μ½”λ“œμž…λ‹ˆλ‹€.

    module.exports = function (next) {
      var paramCounter = 1,
          outerLoopCounter = 0,
          existingSizes = this.chart.sizes.length,
          outerLoopEdge = this.chart.totalSizes,
          innerLoopCounter = 0,
          innerloopEdge = this.chart.totalMeasurements - 1,
          size = {name: null, lengths: [] };

      if (this.req.method && this.req.method === "POST") {
          for (outerLoopCounter;
                  outerLoopCounter < existingSizes;
                  outerLoopCounter = outerLoopCounter + 1) {

              this.chart.sizes[outerLoopCounter].name =
                  this.param("name_" + paramCounter);

              for (innerLoopCounter;
                      innerLoopCounter <= innerloopEdge;
                      innerLoopCounter = innerLoopCounter + 1) {
                  this.chart.sizes[outerLoopCounter].lengths[innerLoopCounter] =
                      this.param(this.chart.measurements[innerLoopCounter].name);
              }
              paramCounter = paramCounter + 1;
              innerLoopCounter = 0;
          }

        for (outerLoopCounter;
                outerLoopCounter < outerLoopEdge;
                outerLoopCounter = outerLoopCounter + 1) {
            size.name = this.param("name_" + paramCounter);
            for (innerLoopCounter;
                    innerLoopCounter < innerloopEdge;
                    innerLoopCounter = innerLoopCounter + 1) {
                size.lengths.push(
                    this.param(this.chart.measurements[innerLoopCounter].name
                               + "_" + paramCounter)
                );
            }
            this.chart.sizes.push(size);
            paramCounter = paramCounter + 1;
            innerLoopCounter = 0;
            size = { name: null, lengths: [] };
        }

        this.chart.save(function (err) {
            if (err) {
                console.log(err);
            }

            this.chart_info = "measurements for <strong>" + this.chart.name + "</strong> saved.";
            this.render("display");
        }.bind(this));
    } else {
        next();
    }
  };

μ‹€μ œλ‘œ 이 ν•„ν„°λ₯Ό μ‹€ν–‰ν•˜κΈ° 전에 μΈ‘μ •κ°’ 및 크기의 2차원 배열을 기반으둜 μƒνƒœ 쒅속 양식 ꡬ쑰λ₯Ό μƒμ„±ν•˜λŠ” ν•„ν„°κ°€ 호좜된 λ‹€μŒ 이 μš”μ†Œ λͺ©λ‘μ„ ꡬ문 λΆ„μ„ν•˜μ—¬ μ—…λ°μ΄νŠΈλ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. 더미 λ°μ΄ν„°λ‘œ ν…ŒμŠ€νŠΈν–ˆμœΌλ©° μ½˜μ†”μ—μ„œ κ°€μ Έμ˜¨ ν˜„μž¬ 더미 μ°¨νŠΈλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

> db.charts.find();
{ "_id" : ObjectId("553da6c3d3d0940a640e878c"), "name" : "Chart With Measurements", "diagram" : "http://res.cloudinary.com/harung71k/image/upload/v1430103747/nd4gipcxnykbbcpcztp9.jpg", "description" : "<p>This is a test of the new measurement methodology, it works.</p>", "totalMeasurements" : 4, "totalSizes" : 3, "sizes" : [ { "name" : "Small", "_id" : ObjectId("554183ed63c5945b73b8a8e7"), "lengths" : [ "1", "2", "3", "4" ] }, { "name" : "Medium", "_id" : ObjectId("554183ed63c5945b73b8a8e8"), "lengths" : [ "5", "6", "7", "8" ] }, { "name" : "Large", "_id" : ObjectId("554183ed63c5945b73b8a8e9"), "lengths" : [ "9", "10", "11", "12" ] } ], "measurements" : [ { "name" : "Fuzz", "instruction" : "<p>Fuzz Instructions</p>", "_id" : ObjectId("553dadd253eb9f996c68a381") }, { "name" : "Buzz", "instruction" : "<p>Buzz Instructions</p>", "_id" : ObjectId("553dadd253eb9f996c68a382") }, { "name" : "Beatles", "instruction" : "<p>Beatles Instructions</p>", "_id" : ObjectId("553dadd253eb9f996c68a383") }, { "name" : "Stones", "instruction" : "<p>Stones instructions</p>", "_id" : ObjectId("553ee7a09ff8c567004bd261") } ], "__v" : 3 }

λ‚˜λŠ” 크기[0].lengths[0] μŠ¬λ‘―μ— "1111"의 μž…λ ₯이 주어지고 μ €μž₯ν•˜κΈ° 직전에 μˆ˜μ •λœ 크기 배열을 ν…ŒμŠ€νŠΈν–ˆκ³  이것은 λ…Έλ“œμ—μ„œ 검사할 λ•Œ λ¬Έμ„œμ— ν‘œμ‹œλ˜μ§€λ§Œ μ €μž₯은 μ •ν™•νžˆ λ°˜ν™˜λ©λ‹ˆλ‹€ 사전 μ €μž₯κ³Ό λ™μΌν•œ λ¬Έμ„œμž…λ‹ˆλ‹€.

μΆ”κ°€ 세뢀정보가 ν•„μš”ν•œ 경우 μ•Œλ €μ£Όμ„Έμš”.

@crispen-smith μ„€λͺ…이 ν•„μš”ν•©λ‹ˆλ‹€. μ½”λ“œκ°€ λ„ˆλ¬΄ λ§Žμ•„μ„œ μ‰½κ²Œ 이해할 수 μ—†μŠ΅λ‹ˆλ‹€. require('mongoose').set('debug', true); λͺ½κ΅¬μŠ€μ˜ 디버그 λͺ¨λ“œλ₯Ό ν™œμ„±ν™”ν•˜κ³  좜λ ₯을 κ²Œμ‹œν•  수 μžˆμŠ΅λ‹ˆκΉŒ? 그러면 μ„œλ²„λ‘œ μ „μ†‘λ˜λŠ” 쿼리와 μ“°κΈ°κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€.

예, λ§Žμ€ μ½”λ“œμ΄μ§€λ§Œ λ™μ‹œμ—... μ‰½κ²Œ μ„€λͺ…ν•˜κΈ°μ—λŠ” μΆ©λΆ„ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ 디버그λ₯Ό μ‹€ν–‰ν•˜λŠ” 것은 μ €μž₯이 μ μ€‘λ˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것을 μ•”μ‹œν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. λ‹€μŒμ€ 이름을 λ³€κ²½ν•œ μ €μž₯μž…λ‹ˆλ‹€.

Mongoose: charts.findOne({ name: 'Chart With Measurements' }) { fields: undefined }  
Mongoose: charts.update({ _id: ObjectId("553da6c3d3d0940a640e878c"), __v: 3 }) { '$set': { 'sizes.0.name': 'Smaller' } } {} 

λ‹€μŒμ€ λ¬Έμžμ—΄ λ°°μ—΄μ—μ„œ 이름과 값을 λ³€κ²½ν•˜μ—¬ λ™μΌν•œ μž‘μ—… μˆœμ„œλ₯Ό μ‹€ν–‰ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

Mongoose: charts.findOne({ name: 'Chart With Measurements' }) { fields: undefined }  
Mongoose: charts.update({ _id: ObjectId("553da6c3d3d0940a640e878c"), __v: 3 }) { '$set': { 'sizes.0.name': 'Small' } } {} 

그리고 λ°°μ—΄ λ‚΄λΆ€μ˜ λ³€μˆ˜λ§Œ λ³€κ²½ν•˜λ©΄ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

λͺ½κ΅¬μŠ€: chart.findOne({ 이름: 'μΈ‘μ • 차트' }) { ν•„λ“œ: μ •μ˜λ˜μ§€ μ•ŠμŒ }
(예, κ·ΈλΏμž…λ‹ˆλ‹€... μ—…λ°μ΄νŠΈ 쿼리 μ—†μŒ)

적어도 당뢄간은 맀번 전체 ν•˜μœ„ λ¬Έμ„œλ₯Ό μ‚­μ œν•˜μ—¬ 패치λ₯Ό μ‹œλ„ν•  κ²ƒμž…λ‹ˆλ‹€. 이 νŠΉμ • λͺ©μ μ„ μœ„ν•΄ μ°Έμ‘° λ¬΄κ²°μ„±μ΄λ‚˜ 개체 IDκ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

--μ—…λ°μ΄νŠΈ--
μ‚­μ œν•˜κ³  μƒˆλ‘œκ³ μΉ¨λ„ ν•΄λ΄€λŠ”λ° μ•ˆλ˜λ„€μš”. 이 쑰각에 λŒ€ν•œ 좜λ ₯ 좔적이 μ—†μ§€λ§Œ μ‚­μ œν•΄λ„ λ°±μ—”λ“œ μž‘μ—…μ΄ νŠΈλ¦¬κ±°λ˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. νŠΈλ¦¬κ±°λ˜μ§€ μ•ŠλŠ” μΌμ’…μ˜ 더티 검사가 μžˆμŠ΅λ‹ˆκΉŒ?

μœ„μ˜ λ‚΄ λ©”λͺ¨λ₯Ό κ°μ•ˆν•  λ•Œ 이것을 λ‹€μ‹œ μ—΄ 수 μžˆλŠ” μ˜΅μ…˜μ΄ μžˆμŠ΅λ‹ˆκΉŒ?

@crispen-smithλŠ” 이 문제λ₯Ό 독립 μ‹€ν–‰ν˜• 슀크립트둜 μž¬ν˜„ν•˜κΈ° μœ„ν•œ 기본적인 μ‹œλ„μž…λ‹ˆλ‹€.

var mongoose = require('mongoose');
mongoose.set('debug', true);
var util = require('util');
var assert = require('assert');

mongoose.connect('mongodb://localhost:27017/gh1204');

var Schema = mongoose.Schema;

var measurement = new Schema({
    name: {type: String, required: true},
    instruction: {type: String, required: true}
});

var size = new Schema({
    name: {type: String, required: true},
    lengths: [String]
});

var chartSchema = new Schema({
    measurements: [measurement],
    sizes: [size]
});

var Chart = mongoose.model('gh1204', chartSchema);

Chart.create({}, function(error, chart) {
  assert.ifError(error);
  chart.sizes.push({ name: 'bacon', lengths: ['25'] });
  chart.save(function(error, chart) {
    assert.ifError(error);
    assert.equal(chart.sizes[0].lengths.length, 1);
    assert.equal(chart.sizes[0].lengths[0], '25');
    console.log('done');
    process.exit(0);
  });
});

아직 μ£Όμ‚¬μœ„κ°€ μ—†μŠ΅λ‹ˆλ‹€. μ˜ˆμƒλŒ€λ‘œ μ‹€ν–‰λ©λ‹ˆλ‹€. 보고 μžˆλŠ” 문제λ₯Ό 보여주기 μœ„ν•΄ μœ„μ˜ 예λ₯Ό μˆ˜μ •ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? λ‚˜λŠ” λ‹Ήμ‹ μ˜ μ‚°λ¬Έ μ„€λͺ…을 μ½”λ“œλ‘œ λ²ˆμ—­ν•  수 μ—†μ—ˆμŠ΅λ‹ˆλ‹€.

λ¬Όλ‘ μž…λ‹ˆλ‹€. 였늘 저녁에 쑰금 이따 μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

μ‹œλ„ν•΄ λ³΄μ‹œκ² μŠ΅λ‹ˆκΉŒ?

/*jslint node:true */
"use strict";

var mongoose = require('mongoose');
mongoose.set('debug', true);
var util = require('util');
var assert = require('assert');

mongoose.connect('mongodb://localhost:27017/gh1204');

var Schema = mongoose.Schema;

var measurement = new Schema({
  name: {type: String, required: true},
  instruction: {type: String, required: true}
  });

var size = new Schema({
  name: {type: String, required: true},
  lengths: [String]
    });

var chartSchema = new Schema({
  measurements: [measurement],
  sizes: [size]
    });

var Chart = mongoose.model('gh1204', chartSchema);

Chart.create({}, function (error, chart) {
    assert.ifError(error);
    chart.sizes.push({ name: 'bacon', lengths: ['25'] });
    chart.save(function (error, chart) {
      assert.ifError(error);
     assert.equal(chart.sizes[0].lengths.length, 1);
     assert.equal(chart.sizes[0].lengths[0], '25');
     console.log('Created Index');

    chart.sizes[0].lengths[0] = "20";

  chart.save(function (error, chart) {
    assert.ifError(error);
    assert.equal(chart.sizes[0].lengths.length, 1);
    assert.equal(chart.sizes[0].lengths[0], '25');
    console.log('Created Index');
    process.exit(0);

    });
  });
});

λ‚΄ λ…Έλ“œ μ½˜μ†”μ˜ μ½˜μ†” 좜λ ₯은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

Crispens-MacBook-Pro:mongooseTest crispensmith$ node index.js
Mongoose: gh1204.insert({ _id: ObjectId("5580d0c44d32b07971dfd281"), sizes: [], measurements: [], __v: 0 })   
Mongoose: gh1204.update({ __v: 0, _id: ObjectId("5580d0c44d32b07971dfd281") }) { '$set': { measurements: [] }, '$pushAll': { sizes: [ { name: 'bacon', _id: ObjectId("5580d0c44d32b07971dfd282"), lengths: [ '25' ] } ] }, '$inc': { __v: 1 } }  
Created Index
Mongoose: gh1204.update({ __v: 1, _id: ObjectId("5580d0c44d32b07971dfd281") }) { '$set': { measurements: [] }, '$inc': { __v: 1 } }  

/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:108
if (this.ended && !this.hasRejectListeners()) throw 이유;
^^
μ–΄μ„€μ…˜ 였λ₯˜: "20" == "25"
EventEmitterμ—μ„œ(/Users/crispensmith/Documents/mongooseTest/index.js:44:14)
EventEmitterμ—μ„œ(/μ‚¬μš©μž/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
EventEmitter.emitμ—μ„œ (events.js:98:17)
Promise.safeEmitμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
Promise.fulfillμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
Promise.resolveμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/lib/promise.js:113:23)
λͺ¨λΈμ—μ„œ.(/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/lib/document.js:1569:39)
at next_ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:89:34)
EventEmitter.fnWrapperμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:171:15)
EventEmitterμ—μ„œ.(/μ‚¬μš©μž/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
EventEmitter.emitμ—μ„œ (events.js:98:17)
Promise.safeEmitμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
Promise.fulfillμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
p1.then.then.self.isNewμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/lib/model.js:254:27)
newTickHandlerμ—μ„œ (/Users/crispensmith/Documents/mongooseTest/node_modules/mongoose/node_modules/mpromise/lib/promise.js:229:18)
process._tickCallbackμ—μ„œ (node.js:442:13)

(μ£„μ†‘ν•©λ‹ˆλ‹€. 이것을 μ μ ˆν•œ λ§ˆν¬λ‹€μš΄μœΌλ‘œ λ§Œλ“œλŠ” 데 μ‹œκ°„μ΄ 많이 κ±Έλ ΈμŠ΅λ‹ˆλ‹€. 이것이 μ œκ°€ ν•  수 μžˆλŠ” μ΅œμ„ μž…λ‹ˆλ‹€.)
λ‹€μŒμ€ mongoDB 좜λ ₯μž…λ‹ˆλ‹€.

  > db.gh1204.find();
  { "_id" : ObjectId("5580d0c44d32b07971dfd281"), "sizes" : [ { "name" : "bacon", "_id" : ObjectId("5580d0c44d32b07971dfd282"), "lengths" : [ "25" ] } ], "measurements" : [ ], "__v" : 2 }

CRUD κ΄€μ μ—μ„œ 핡심은 Create λ˜λŠ” Reads, put Updates μ‹€νŒ¨μ— λ¬Έμ œκ°€ μ—†λ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

TLDR;
제 μ‚¬μš© μ‚¬λ‘€μ—μ„œ 이것은 츑정에 λŒ€ν•΄ 길이 μ„ΈνŠΈκ°€ μ •μ˜λ˜λ©΄ νŽΈμ§‘ν•  수 μ—†μŒμ„ μ˜λ―Έν•©λ‹ˆλ‹€. μ €λŠ” ν•˜μœ„ λ¬Έμ„œ 내에 배열이 ν•„μš”ν•œ 두 가지 μ‚¬μš© 사둀가 μžˆλŠ” νŒ¨μ…˜ μ†Œλ§€ ν”„λ‘œμ νŠΈλ₯Ό 진행 μ€‘μž…λ‹ˆλ‹€. 첫 번째 μ‚¬μš© μ‚¬λ‘€λŠ” 바닐라 μ‚¬μš©μžκ°€ μžμ‹ μ˜ ν”„λ‘œν•„μ„ κ°–κ³  이λ₯Ό μœ μ§€ν•  수 μžˆμ–΄μ•Ό ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. 두 번째 μ‚¬μš© μ‚¬λ‘€λŠ” κ΄€λ¦¬μž 역할이 λ‹€μ–‘ν•œ μ œν’ˆ μœ ν˜•(상단, ν•˜λ‹¨ λ“±)에 λŒ€ν•œ λͺ©λ‘μ— λŒ€ν•œ μ‚¬μš©μž 지정 차트λ₯Ό 생성할 수 μžˆλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. 일반적으둜 κ΄€λ¦¬μž ν”„λ‘œν•„μ΄ μ΄λŸ¬ν•œ 보쑰 차트λ₯Ό νŽΈμ§‘ν•΄μ•Ό ν•  κ²ƒμœΌλ‘œ μ˜ˆμƒν•˜μ§€λŠ” μ•Šμ§€λ§Œ ν•΄λ‹Ή μ‚¬μš© μ‚¬λ‘€μ—λŠ” μ—¬μ „νžˆ 있으면 쒋을 κ²ƒμž…λ‹ˆλ‹€.

이둠적으둜 ν˜„μž¬ κΈ°λŠ₯은 μ‹€μ œλ‘œ 멋진(μš°μ—°ν•œ) λ³€κ²½ν•  수 μ—†λŠ” 개체λ₯Ό μ œκ³΅ν•˜μ§€λ§Œ λ‹¨μˆœνžˆ νŽΈμ§‘ν•˜λŠ” 것이 μ•„λ‹ˆλΌ λ³€κ²½ν•  수 μ—†λŠ” 개체둜 μ‚¬μš©ν•˜λŠ” 데 μˆ˜λ°˜λ˜λŠ” μž‘μ—…μ˜ 양은 μ‚¬μ†Œν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λͺ½κ΅¬μŠ€ FAQ 에 λŒ€ν•œ 첫 번째 질문 의 κ³ μ „ 적인 사둀 κ°€ 무엇 인지 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€ . MongooseλŠ” ES6 ν”„λ‘μ‹œ λ˜λŠ” ES7 Object.observe() 없이 λ°°μ—΄ 인덱슀λ₯Ό 직접 μ„€μ •ν•  λ•Œ λ³€κ²½ 사항을 좔적할 수 μ—†μŠ΅λ‹ˆλ‹€. μ‚¬μš©ν•˜λ‹€

chart.sizes[0].lengths.set(0, "20");

λ˜λŠ”

chart.sizes[0].lengths[0] = '20';
chart.markModified('sizes.0.lengths.0');

μ•Œκ² μŠ΅λ‹ˆλ‹€. μ΄μƒν•˜κ²Œλ„(λ‚΄ 검색 μ „λž΅ λ•Œλ¬ΈμΌ 수 있음) μ €μž₯λ‹Ή 1xμ”© μž‘λ™ν•˜λŠ” κΈ°λŠ₯만 찾을 수 μžˆμ—ˆκ³  κ·Έ 쀑 μ–΄λŠ 것도 μ‚¬μš© 사둀에 λ§žμ§€ μ•Šμ•˜μ„ κ²ƒμž…λ‹ˆλ‹€.

λ¬Έμ œκ°€ 무엇인지 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. λͺ½κ΅¬μŠ€ FAQ에 λŒ€ν•œ 첫 번째 질문의 μ „ν˜•μ μΈ μ‚¬λ‘€μž…λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€. 제 κ²½μš°μ—λŠ” 이 λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€! :)

model.myArray[index] = anyValue

λœλ‹€

model.myArray.set(index, anyValue)

이것은 λ˜ν•œ μ €μ—κ²Œ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€. 이 μŠ€λ ˆλ“œλ₯Ό 더 빨리 봀으면 μ’‹κ² μŠ΅λ‹ˆλ‹€!

λͺ½κ΅¬μŠ€κ°€ λ³€κ²½ 사항에 λŒ€ν•œ μ•Œλ¦Όμ„ λ°›λŠ”μ§€ ν™•μ‹€ν•˜μ§€ μ•Šμ€ 경우 λ‹€μŒμ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

doc.markModified('propChanged')
doc.save() // works

Array.set()μ—μ„œλ„ λ‚΄ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.
:+1:

같은 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. 이것은 λ‚΄ μ½”λ“œ μŠ€λ‹ˆνŽ«μž…λ‹ˆλ‹€.
image
그리고 이것은 λͺ½κ΅¬μŠ€ λ‘œκ·Έμž…λ‹ˆλ‹€.
image
이것은 λ‚΄κ°€ 우편 λ°°λ‹¬λΆ€μ—κ²Œμ„œ 얻은 κ²ƒμž…λ‹ˆλ‹€.
image

λ¬Έμ œκ°€ μžˆλ‚˜μš”?

++ νŽΈμ§‘ ++

  1. Array.set을 μ‚¬μš©ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ λ™μΌν•œ κ²°κ³Όλ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 더 λ‚˜μ€ 질문이 μ˜³μ€ 것이 μžˆλŠ”μ§€ μƒκ°ν•©λ‹ˆλ‹€. if (req.body.invite != []) λΈ”λ‘μ—μ„œ 비동기 μ €μž₯을 μˆ˜ν–‰ν•œ λ‹€μŒ resolve μ½œλ°±μ—μ„œ invited 배열을 μˆ˜μ •ν•©λ‹ˆλ‹€. nRoom.invited.push() ν˜ΈμΆœμ€ 항상 두 번째 save() 호좜 후에 λ°œμƒν•©λ‹ˆλ‹€. λ˜ν•œ req.body.invite != [] λŠ” JSμ—μ„œ [] == [] κ°€ false이기 λ•Œλ¬Έμ— 항상 trueμž…λ‹ˆλ‹€.

@vkarpov15 ν•˜ν•˜

@peterkrieg κ°μ‚¬ν•©λ‹ˆλ‹€!

μ•ˆλ…•ν•˜μ„Έμš”, 깊이 ν¬ν•¨λœ λ¬Έμ„œλ₯Ό μ—…λ°μ΄νŠΈν•  λ•Œ 이 λ¬Έμ œκ°€ λ°œμƒν•©λ‹ˆλ‹€. 쑰언이 μžˆμœΌμ‹ κ°€μš”? λ‚΄ μ½”λ“œλŠ” λ‹€μŒ μœ„μΉ˜μ— μžˆμŠ΅λ‹ˆλ‹€.

https://stackoverflow.com/questions/51426326/updating-deeply-embedded-documents-with-mongodb-2-6-12

@thehme μ–΄λ–€ λ²„μ „μ˜ λͺ½κ΅¬μŠ€λ₯Ό μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ?

이 μ€„μ—μ„œ {$set: {["dT." + index +".ts." + i + ".th"]: newValue}} , λŒ€κ΄„ν˜ΈλŠ” λ‚˜μ—κ²Œ μ ν•©ν•˜μ§€ μ•Šμ€ λŠλ‚Œμž…λ‹ˆλ‹€. λŒ€μ‹  {$set: { "dT." + index +".ts." + i + ".th": newValue } } λ₯Ό μ‚¬μš©ν•˜λŠ” 것이 차이가 μžˆμŠ΅λ‹ˆκΉŒ?

$set λ¬Έμ„œλŠ” λ¬Έμžμ—΄λ§Œ ν‘œμ‹œν•©λ‹ˆλ‹€.

Gitter.im λ˜λŠ” Slack 에 κ°€μž…ν•˜μ—¬ μ‹€μ‹œκ°„μœΌλ‘œ λŒ€ν™”λ₯Ό λ‚˜λˆ„μ„Έμš” πŸ‘

5.5.11을 μ‚¬μš©ν•˜κ³  μžˆλŠ”λ° μ—¬μ „νžˆ 이 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” 운이 없이 이 μŠ€λ ˆλ“œμ—μ„œ μ œμ•ˆλœ λͺ¨λ“  μ†”λ£¨μ…˜μ„ μ‹œλ„ν–ˆμŠ΅λ‹ˆλ‹€. λ‚΄ λ¬Έμ„œ λ ˆμ΄μ•„μ›ƒμ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

{
  myObj: {
    myArr: [{ key: "value" }]
  }
}

myDoc.myObj.myArr.push({ key: "value2" });
// debugging myDoc shows new embedded doc
await myDoc.save();
// console shows new embedded doc is gone

νŽΈμ§‘: μ΅œμ‹ (5.7.6)으둜 μ—…κ·Έλ ˆμ΄λ“œν–ˆμ§€λ§Œ μ—¬μ „νžˆ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

μ•„λž˜ μŠ€ν¬λ¦½νŠΈλŠ” Mongoose 5.7.6μ—μ„œ 잘 μž‘λ™ν•©λ‹ˆλ‹€. μƒˆ 문제λ₯Ό μ—΄κ³  문제 ν…œν”Œλ¦Ώμ„ λ”°λ₯΄μ„Έμš”.

const mongoose = require('mongoose');

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  });
  await mongoose.connection.dropDatabase();

  const schema = mongoose.Schema({ 
    myObj: { myArr: [{ key: String }] }
  });
  const Model = mongoose.model('Test', schema);


  await Model.create({ myObj: { myArr: [{ key: 'value' }] } });
  const myDoc = await Model.findOne();

  myDoc.myObj.myArr.push({ key: "value2" });
  await myDoc.save();

  console.log(myDoc.myObj.myArr);

  const saved = await Model.findOne();
  console.log(myDoc.myObj.myArr);
}
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰