var FooSchema = new Schema({});
var Foo = mongoose.model("Foo", FooSchema);
var BarSchema = new Schema({
foos: [Foo.schema]
});
var Bar = mongoose.model("Bar", BarSchema);
var b = new Bar();
b.save();
์์ฑ์ ์ ์ํ์ง ์์ ์ํ๋ก ๋๋ ๋์ ์ ๋น b.foos ๋ฐฐ์ด์ด ์์ฑ๋ฉ๋๋ค.
์ด๊ฒ์ ์๋์ ์ผ๋ก ์ค๊ณ๋ ๊ฒ์ ๋๋ค. ๋ฌธ์์ ๋ชฝ๊ตฌ์ค ์ ํ๋ฆฌ์ผ์ด์ ๋ณด๊ธฐ๊ฐ ๋น๋ชฝ๊ตฌ์ค ์ ํ๋ฆฌ์ผ์ด์ ๋ณด๊ธฐ์ ๋์ผํ๋๋ก ๋ชจ๋ ๊ธฐ๋ณธ๊ฐ์ด ์ ์ฅ๋ฉ๋๋ค.
์ข์, ์ด๋ป๊ฒ ๊ธฐ๋ณธ null์ ๋ง๋ค ์ ์์ต๋๊น? ;-) ์ฌ๊ธฐ์์ ์ฌ์ฉ ์ฌ๋ก๋ ๋น foo๊ฐ ์๋ ๋ชจ๋ Bar๋ฅผ ์์ฒญํ๋ ๊ฒ์ด mongo์์ ๋น์ฉ์ด ๋ง์ด ๋ ๋ค๋ ๊ฒ์ ๋๋ค. ํด๋น ์ํ๋ฅผ ์ถ์ ํ๊ธฐ ์ํด ๋ค๋ฅธ ์์ฑ์ ์ถ๊ฐํด์ผ ํ๋ฏ๋ก ์ฝ๋๊ฐ ๋ ๋ณต์กํด์ง๋๋ค.
์ ๊ฒ ์ด์. ์ด๋ ๊ฒ ํ๋ฉด ์ ๋ฌธ์์ ๋น foo๋ฅผ ์ ์ฅํ๋ ๊ฒ์ ๊ฑด๋๋ธ ์ ์์ต๋๋ค.
BarSchema.pre('save', function (next) {
if (this.isNew && 0 === this.foos.length) {
this.foos = undefined;
}
next();
})
๋ฌ์ฝคํ. ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์ถฉ๋ถํ ์ข์ ์ผ์ ๋๋ค.
์ด ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์ฌ์ ํ ์๋ํด์ผ ํฉ๋๊น? (์ฌ๊ธฐ ๋ชฝ๊ตฌ์ค 3.8.3)
Mathieumg: ๊ทธ๊ฒ์ ๋๋ฅผ ์ํด ์๋ํ์ง ์์ต๋๋ค (3.8.15)
์, ์ด ๋ฌธ์ ๋ ์ฃผ์๋ฅผ ๊ธฐ์ธ์ฌ์ผ ํ๋ค๊ณ ์๊ฐํฉ๋๋ค. (์ฌ์ํ์ง๋ง..
๋๋ ๋ค๋ฅธ ๊ฒ๋ค์ pre๋ฅผ ์ฌ์ฉํ๋ ๋ฐ ๊ด์ฌ์ด ์์ต๋๋ค :) )
2014๋
8์ 25์ผ ์์์ผ ์คํ 4์ 49๋ถ, Mathieu M-Gosselin <
[email protected]>์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ์์์ฐจ๋ฆฌ๊ธฐ ์ํด ์ ๋ฌธ์ ๋ฅผ ์ด์ด์ผ ํฉ๋๊น?
โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
https://github.com/LearnBoost/mongoose/issues/1335#issuecomment -53328195
.
+1
์ฌ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ 3.8.15์์ ์๋ํ์ง ์์ต๋๋ค.
๋ด๊ฐ ์๊ฐํ๋ ์ ์ผํ ๋์์ ์ฟผ๋ฆฌ๋ฅผ ๋จ์ํํ๊ธฐ ์ํด null ๋๋ false๋ฅผ ๊ฐ๋ ๊ฒ์ด์ง๋ง ์ผ๋ถ ๋ฐ์ดํธ๋ฅผ ์ถ๊ฐํ๊ณ ํฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋นํ ์ํฅ์ ๋ฏธ์น๋ ๊ฒ์
๋๋ค.
this.foos = null;
@gaurang171 @Nepoxx ์ด๊ฑฐ ์ฌํํ๋ ์ฝ๋ ์ข ์๋ ค ์ฃผ์ค ์ ์๋์? ํด๊ฒฐ ๋ฐฉ๋ฒ์ ํ ์คํธ ์ผ์ด์ค์์ ์ ์๋ํฉ๋๋ค( ํ ์คํธ ์ผ์ด์ค ์ฐธ์กฐ).
์ด๊ฒ์ 4.0.1์ ์ฌ์ฉํ๋ ์ ์๊ฒ ์ฌ์ ํ ๋ฌธ์ ์ ๋๋ค.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var barSchema = new mongoose.Schema({
baz: String
});
var fooSchema = new mongoose.Schema({
bars: [barSchema]
});
var Foo = mongoose.model('Foo', fooSchema);
var foo = new Foo();
console.log(foo); // { _id: 55256e20e3c38434687034fb, bars: [] }
foo.save(function(err, foo2) {
console.log(foo2); // { __v: 0, _id: 55256e20e3c38434687034fb, bars: [] }
foo2.bars = undefined;
foo2.save(function(err, foo3) {
console.log(foo3); // { __v: 0, _id: 55256e20e3c38434687034fb, bars: undefined }
Foo.findOne({ _id: foo3._id }, function(err, foo4) {
console.log(foo4); // { _id: 55256e20e3c38434687034fb, __v: 0, bars: [] }
mongoose.disconnect();
});
});
});
@viking ๊ทธ๊ฒ์ ๊ฒฐ์ฝ '๊ณ ์ '๋์ง ์์์ต๋๋ค. ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์ ๊ณต๋์์ต๋๋ค . ์ด์ ๋ํด ์ถฉ๋ํ๋ ์ ๋ณด๊ฐ ์์ผ๋ฏ๋ก ํด๋น ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์ฌ์ ํ ์๋ํ๋์ง ํ ์คํธํด์ผ ํฉ๋๋ค. =)
์ด๋ฏธ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ด ๋์์ด ๋ณ๊ฒฝ๋๋์ง ์ฌ๋ถ๋ฅผ ์๊ณ ์ถ์ต๋๋ค.
์ฃ์กํฉ๋๋ค. ํ๋์ด ๋ณ๊ฒฝ๋๋์ง ์ฌ๋ถ๋ฅผ ์๊ณ ์ถ๋ค๊ณ ๋ง์ํ์ง ์์ผ์ จ์ต๋๋ค.
์ค์ ๋ก ์ด ๋ฌธ์ ๊ฐ @vkarpov15์ ์ํด ๋ค์ ์ด๋ฆฐ ์ด์ ๊ฐ ํ์คํ์ง ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ ์ด ๋์์ด ๋ณ๊ฒฝ๋ ์ง ์ฌ๋ถ์ ๋ํ ์ง๋ฌธ์ ์ ๊ธฐํฉ๋๋ค. ๋ฌธ์ ๋ ์ด์ ๋ฒ์ ๊ณผ์ ํธํ์ฑ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ์๋ง๋ 4.x๊ฐ ๋์ค๊ธฐ ์ ์ ๊ฒฐ์ ๋์์ ๊ฒ์ ๋๋ค.
์, ์ด ๋์์ ๊ฐ๊น์ด ์ฅ๋์ ๋ณ๊ฒฝ๋์ง ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ๋ค์ ์ด์์ผ๋ฏ๋ก ๊ธฐํ๊ฐ ์์ ๋ ์กฐ์ฌํด์ผ ํ๋ค๋ ๊ฒ์ ์์์ต๋๋ค. ์๋ํ๋ฉด mongoose github์ ๋ค์ด์ค๋ ๋ชจ๋ ๋๊ธ์ ๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๋์ง ํ์คํ ์ดํดํ๊ณ ์๊ณ ์ด๊ฒ์ ์ข์ ๋ต๋ณ์ด ์๋ ๋ฌธ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ด ๋จธ๋ฆฌ ์๋ก. ๋ ์์ธํ ์ดํด๋ณด๋ฉด semver ๋๋ฌธ์ ๊ฐ๊น์ด ์์ผ ๋ด์ ์ค์ ๋ก ๋ณ๊ฒฝํ ์๋ ์์ง๋ง ๋ง์ ์ฌ์ฉ์์๊ฒ ๊ณ ์ถฉ์ด ์๋์ง ์กฐ์ฌํด์ผ ํฉ๋๋ค.
๋๋ ๋๋๋ก ๋น์ ์ด ๋น ๋ฐฐ์ด์ ์์ฑํ๊ณ ์ถ์ดํ๊ณ ๋๋ก๋ ๊ทธ๋ ์ง ์๊ธฐ๋ฅผ ์ํ๊ธฐ ๋๋ฌธ์ ๋นํ๋ฆผ๊ณผ ํจ๊ป ์ด ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ์ต๋๋ค.
๋ด ์ฌ์ ์ ์ฅ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
var schema = mongoose.Schema({
...
"children": [ String ]
...
});
// turn off the saving of empty children if there are no children in the schema.
schema.pre('save', function (next) {
if (this.isNew) {
if (this.children.length == 0) {
this.children = undefined;
}
else if (this.children.length == 1 && this.children[0] == null) {
this.children = [];
}
}
next();
});
var model = mongoose.model('MyDocument', schema);
๊ทธ๋ฐ ๋ค์ ๋น ์์์ ์ฐ๊ธฐ ์ํด ๋ค์์ ์ฌ์ฉํฉ๋๋ค.
idea.children = [null];
์ด๊ฒ์ ์ ๊ฑฐ๋๋ ๋ฐ๋ฉด:
idea.children = [];
์ด๊ฒ์ ๋น์ ์๊ฒ ํจ๊ณผ๊ฐ ์์ ์๋ ์์ง๋ง ๋ค๋ฅธ ์ ํ์ ๊ณํ์ ์๊ฐํด๋ผ ์ ์์ด์ผ ํฉ๋๋ค.
์ค์ฒฉ๋ ๊ฐ์ฒด ๋ด๋ถ์ ๋ฐฐ์ด์ด ์์ ๋ ์ด๊ฒ์ ๊ฝค ํฐ ๋ฌธ์ ์ ๋๋ค.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var quxSchema = new mongoose.Schema({ qux: String });
var fooSchema = new mongoose.Schema({ foo: { bar: { baz: [ quxSchema ] } } });
var Foo = mongoose.model('Foo', fooSchema);
var foo = new Foo({foo: undefined})
foo.save(function(err, foo) {
console.log(JSON.stringify(foo)); // {"__v":0,"_id":"557ae56480f047fd4ff4ab26","foo":{"bar":{"baz":[]}}}
Foo.find({ _id: foo._id }, function(err, foos) {
console.log(JSON.stringify(foos[0])); // {"_id":"557ae56480f047fd4ff4ab26","__v":0,"foo":{"bar":{"baz":[]}}}
});
});
์ฌ๊ธฐ์ foo
๊ฐ ๋๋ฝ๋ ๊ฒ์ผ๋ก ์์ํ๊ณ ๋์ ๋น ๋ฐฐ์ด์ด ํฌํจ๋ ์ด ์ค์ฒฉ ๊ฐ์ฒด๊ฐ ์์ต๋๋ค.
ํ์ง๋ง ์ฌ๊ธฐ์ ์ฌ์ ์ ์ฅ์ด ๋น์ ์ ๋์ธ ์ ์์ต๋๊น? {foo: undefined} ๋์ ๊ณ ์ ํ ์ ํ์ foo๋ฅผ ์ฌ์ฉํ์ฌ null foo๋ฅผ ํ์ํ ๋ค์ ์ฌ์ ์ ์ฅ์์ foo๋ฅผ ์ ๊ฑฐํ ์ ์์ต๋๋ค.
๋ชป์๊ฒผ์ง๋ง.
@viking ๊ณผ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์ ์ฉํ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ฐพ์์ต๋๊น?
'์ฌ์ ' ์ ์ฅ ๋ฐฉ์์ผ๋ก ์๋ํ์ง๋ง ์ฌ์ ํ ๊ฐ๋ ๋ฐ์ํฉ๋๋ค. ์ด ์์ ์ ์ํํ๊ธฐ ์ํด ์ผ๋ง๋ ๋ง์ ๋ค๋ฅธ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ๋ค๋ฃจ์ด์ผ ํ๋์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ์ด ์ค ๊ฐ์ฅ ํฐ ๋ฌธ์ ๋ JSON์ด ์ค์ ๋ก ๋ฐฐ์ด์ด ์๋ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค. ์ด๊ฒ์ ์ด๋ฌํ ๊ฒ๋ค์ Java ๊ฐ์ฒด์ ๋งคํํ๋ ค๊ณ ์๋ํ๋ ๋ด json ํ์์ ๋ํ ์น๋ช ์ ์ธ ์์ธ์ ๋๋ค.
์์:
var subA = mongoose.Schema({
name: String,
a: String
});
var subB = mongoose.Schema({
name: String,
b: String
});
var A = mongoose.Schema({
name: String,
filter: {
lastupdate : { type: Date, default: Date.now },
subA: [{type: Schema.Types.ObjectId, ref: 'subA', unique: true}],
subB: [{type: Schema.Types.ObjectId, ref: 'subB', unique: true}]
}
});
var ModelA = mongoose.model('A', A);
var obj = new modelA();
obj.save();
... ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
{
filter: []
}
ํ์ง๋ง ์กด์ฌํ์ง ์๊ฑฐ๋ ์ต์ํ ๊ฐ์ฒด์ด๊ณ ๋น ๋ฐฐ์ด์ด ์๋์ด์ผ ํฉ๋๋ค.
{
filter: {}
}
๋๊ตฐ๊ฐ ์ด๊ฒ์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ด๋์ด๊ฐ ์์ต๋๊น?
๋ฌธ์ ์ธ์ฌ
๋ด๊ฐ ๊ฒ์ํ ์ฌ์ ์ ์ฅ์ด ์๋ ์ด ์์ ์ ์ํํ ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค. ์๋ํ๋ฉด ์์์ด ๊ฐ์ฒด๊ฐ ์๋ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋๋ ์ด๊ฒ์ ์๋ํ์ง ์์์ง๋ง ์ค์ ๋ก "์ค๋ฆฌ ํ์ดํ"Empty ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ๋น ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด ๊ฐ๋ฅํ์ง ์์๊น์?
๋ฐ๋ผ์ ์ฌ์ ์ ์ฅ์ ๋ค์๊ณผ ๊ฐ์ ์ ์์ต๋๋ค.
if (this.isNew) {
if (typeof(this.filter.empty) != "undefined") {
this.filter = {};
}
}
๊ทธ๋ฐ ๋ค์ ์ค์ ๋น ํํฐ๋ฅผ ๋ง๋ค๋ ค๋ฉด ๋ค์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
var obj = new modelA();
obj.filter = { empty: true };
obj.save();
์คํค๋ง์ "๋น์ด ์์"์ด ์์ผ๋ฉด ์ค์ ๋ก ์ค์ํ์ง ์์ผ๋ฉฐ ๊ฒฐ์ฝ ๋ชฝ๊ตฌ์ค๋ก ๋ง๋ค์ง ์์ต๋๋ค.
์ด๋ป๊ฒ ๋ ๋ฌธ์ ๋ฅผ ๊ทน๋ณตํ๋ ๋ฐฉ๋ฒ์ ๊ด๋ฆฌํ์ต๋๋ค .. ์ค์ ๋ก ๋งค์ฐ ์ ์ฌํ ์์ ์ ์ํํ์ต๋๋ค. ๊ทธ๋๋ ๋ฐฉ๊ธ ๋ต๋ณ์ ๋ณด์๊ณ ๊ฐ์ฌ ์ธ์ฌ๋ฅผ ์ ํ๊ณ ์ถ์์ต๋๋ค. :)
๋ฌธ์ ์ธ์ฌ
์๋ชฌ
์ ํ์ Array
ํ๋๊ฐ ์๋ ์ค์ฒฉ ์คํค๋ง๊ฐ ์์ ๋ ์ด ์๋ฃจ์
์ ๋ง์ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค. ๋๋ ์๋ก์ด ์ ํ์ ๋ง๋ค์ด ์ด๊ฒ์ ํด๊ฒฐํ์ต๋๋ค.
optional_array =
type: Mixed
validate:
validator: (v) ->
return v instanceof Array
message: '{VALUE} needs to be an array.'
๊ทธ๋ฐ ๋ค์ ๋ชจ๋ ํ๋๋ฅผ optional_array
๋์ Array
ํฉ๋๋ค.
@simllll ๋๋ฝ๋ ๊ฐ์ฒด์ null
์๋ฆฌ ํ์์๋ฅผ ์ฌ์ฉํ ๋ค์ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ธฐ ์ ์ null
๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
๋ฏธ์ํ์ง๋ง ์ด๊ฒ์ ๋์ฐํ ๋์์ธ์ ๋๋ค. ๋ด๊ฐ ๋น ๋ฐฐ์ด์ด ๋๊ธฐ๋ฅผ ์ํ๋์ง ์๋๋ฉด ์ ํ ์ค์ ํ์ง ์๊ธฐ๋ฅผ ์ํ๋์ง ๋๋ฅผ ๋์ ํ์ฌ ๊ฒฐ์ ํด์๋ ์ ๋ฉ๋๋ค. ์ต์ํ ์ด ๊ธฐ๋ฅ์ ๋นํ์ฑํํ๋ ์ต์ ์ ์ ๊ณตํ์ญ์์ค.
@ifeltsweet์ ๋์
๋ช ๊ฐ์ ํ ํ๋ฆฌํ ์๋ ์ ์ ํ ์๋ฃจ์ ์ด ์์ต๋๊น? ๋๋ ์์ญ ๊ฐ์ ๋ฐฐ์ด ํ๋๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ ํ๋ฆฌ ํ ์์ ํ๋์ฉ ์ฒ๋ฆฌํด์ผํฉ๋๋ค ...
@antonioaltamura ์ด ๋ฌธ์ ๋ฅผ ์์ด๋ฒ๋ ธ์ต๋๋ค. ์๋๋ mongoose 4.6์์ ์๋ํฉ๋๋ค.
const CollectionSchema = new Schema({
field1: { type: [String], default: void 0 }, // <-- override the array default to be undefined
});
const Collection = mongoose.model('test', CollectionSchema);
Collection.create({}).
then(doc => { console.log(doc); return doc; }).
then(() => { process.exit(0); });
@ vkarpov15 mongoose 4.7.0์์ ์์ ์ฌํญ์ด ์๋ํ์ง ์์ต๋๋ค. ์ฌ์ ํ ์ ์๋ํฉ๋๊น? ์ ๊ฐ์ฒด ์์ฑ์ ๋ํ ๊ฐ์ ํฌํจ๋์ง ์์ง๋ง Model.save๋ฅผ ์ํํ๋ฉด ๋ค์ ์ถ๊ฐํ๋ ค๊ณ ํฉ๋๋ค.
@cpup22 ์ฝ๋ ์ํ์ ์ ๊ณตํ ์ ์์ต๋๊น? ์ฐธ์กฐ๋ ์คํฌ๋ฆฝํธ ๋ 4.7.1์์ ์ ์๋ํฉ๋๋ค.
https://github.com/Automattic/mongoose/issues/2363#issuecomment -171988413์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ด ํจ์ฌ ๊นจ๋ํ๋ค๊ณ โโ์๊ฐํฉ๋๋ค. default: null
๊ฐ ์คํจํ๋๋ผ๋ null
๊ธฐ๋ณธ๊ฐ์ ํ์ฉํฉ๋๋ค.
const CollectionSchema = new Schema({
field1: { type: [String] },
});
CollectionSchema.methods.postconstructed = function () {
this.field1 = null;
};
CollectionSchema.queue('postconstructed');
const Collection = mongoose.model('test', CollectionSchema);
Collection.create({}).then(doc => { console.log(doc); process.exit(0) });
์ ๊ฒฝ์ฐ์ง ๋ง์ธ์. ๊ฐ์ ์ ๋ฌํ ๋๋ ์๋ํ์ง ์์ต๋๋ค(์: new Collection({ field1: [] })
๋ field1 === null
๊ฐ์ ธ์ต๋๋ค. ์ ๋ฌ๋ ๋ฐ์ดํฐ๊ฐ ์ ์ฉ๋๊ธฐ ์ ๊ณผ ๊ธฐ๋ณธ๊ฐ ์ดํ์ ๋ฐ์ํ ๊ตฌ์ฑ ํํฌ๋ง ์์๋ค๋ฉด ๋น ๋ฐฐ์ด์ด ์์ฑ๋๊ฑฐ๋ ๋ฐฐ์ด์ default: null
๋ฅผ ํ์ฉํ๋๋ก ์ด ๋ฒ๊ทธ๊ฐ ์์ ๋์์ต๋๋ค.
์ค, ๋ค์๊ณผ ๊ฐ์ด ์๋ํฉ๋๋ค.
const CollectionSchema = new Schema({
field1: { type: [String], default: () => null },
});
๋ฐฐ์ดํ์ ๊ฐ๋ณด๋ค ๊ธฐ๋ฅ์ ๋ ์กด์คํฉ๋๋ค. ์ด๊ฒ์ ์ ๋ฌ๋ null
, []
๋๋ Collection.create()
๋๋ new Collection()
๋ฅผ ํตํด ๋น์ด ์์ง ์์ ๋ฐฐ์ด ๊ฐ์ ๋ฎ์ด์ฐ๋ ๋ฐ ๋ฌธ์ ๊ฐ ์์ต๋๋ค.
@vkarpov15 ์๋
ํ์ธ์.
๋น์ ์ ์ด๋ค ํด๊ฒฐ์ฑ
์ด ์์ต๋๋ค
@vikramkalta ์ฝ๋ ์ํ์ ์ ๊ณตํ์ญ์์ค. ์ฐ๋ฌธ์์ ์ฝ๋๋ก์ ๋ณํ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ธฐ ์ฝ์ต๋๋ค.
์๋ ํ์ธ์, ์ ๋ mongoose 4.6.5๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ ์ฌ์ ํ ์ด๋ฌํ ์๋ฃจ์ ์ผ๋ก ๋น ๋ฐฐ์ด์ ์ ์ฅํฉ๋๋ค.
services: [{
type: Schema.Types.ObjectId,
ref: 'Service',
default: () => null
}],
services: [{
type: Schema.Types.ObjectId,
ref: 'Service',
default: void 0
}],
@keyboard99 https://github.com/Automattic/mongoose/issues/1335#issuecomment -252129243 ์ ์ง์นจ์ ๋ฐ๋ฅด์ธ์. ๊ทํ์ ์ฝ๋๋ ๋ฐฐ์ด ์์ฒด๊ฐ ์๋ ๊ฐ๋ณ ๋ฐฐ์ด ์์์ ๋ํ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํฉ๋๋ค.
๊ทธ ๋ฌธ์ ์ ๋ํ ๊ฐ๋จํ ํด๊ฒฐ์ฑ ์ด ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ ์๋์ง ์์์ผ๋ก ์ ๊ณตํ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค. ๋ค์๊ณผ ๊ฐ์ด:
var BarSchema = new Schema({
foos: {
type: [FooSchema],
default: undefined
}
});
๋๋ฅผ ์ํด ์ผํ๋ค
๊ทธ๋ฌ๋ ์ด๋ array.push์ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ๊ฒ์
๋๋ค.
:(
@GeneralG ๋ฐ์ ๋ช
๋ฐฑํ ์ด์ธ์ ํฉ๋ณ์ฆ์ดํด์ผ ํ ์ผ doc.array = doc.array || [];
ํธ์ถํ๊ธฐ ์ ์ push()
?
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
@antonioaltamura ์ด ๋ฌธ์ ๋ฅผ ์์ด๋ฒ๋ ธ์ต๋๋ค. ์๋๋ mongoose 4.6์์ ์๋ํฉ๋๋ค.