配列
MongooseArray.prototype.$pop()
MongooseArray.prototype.$shift()
MongooseArray.prototype.addToSet()
MongooseArray.prototype.includes()
MongooseArray.prototype.indexOf()
MongooseArray.prototype.inspect()
MongooseArray.prototype.nonAtomicPush()
MongooseArray.prototype.pop()
MongooseArray.prototype.pull()
MongooseArray.prototype.push()
MongooseArray.prototype.remove()
MongooseArray.prototype.set()
MongooseArray.prototype.shift()
MongooseArray.prototype.sort()
MongooseArray.prototype.splice()
MongooseArray.prototype.toObject()
MongooseArray.prototype.unshift()
MongooseArray.prototype.$pop()
参照
ドキュメントの`save()`ごとに最大1回、アトミックに配列の末尾要素を取り除きます。
注意
保存前に配列に対して複数回呼び出しても、1回呼び出した場合と同じコマンドが送信されます。 この更新は、MongoDBの$popメソッドを使用して実装されており、この制限が適用されます。
doc.array = [1,2,3];
const popped = doc.array.$pop();
console.log(popped); // 3
console.log(doc.array); // [1,2]
// no affect
popped = doc.array.$pop();
console.log(doc.array); // [1,2]
doc.save(function (err) {
if (err) return handleError(err);
// we saved, now $pop works again
popped = doc.array.$pop();
console.log(popped); // 2
console.log(doc.array); // [1]
})
MongooseArray.prototype.$shift()
参照
ドキュメントの`save()`ごとに最大1回、アトミックに配列の先頭要素を取り除きます。
注記
保存前に配列に対して複数回呼び出しても、1回呼び出した場合と同じコマンドが送信されます。 この更新は、MongoDBの$popメソッドを使用して実装されており、この制限が適用されます。
doc.array = [1,2,3];
const shifted = doc.array.$shift();
console.log(shifted); // 1
console.log(doc.array); // [2,3]
// no affect
shifted = doc.array.$shift();
console.log(doc.array); // [2,3]
doc.save(function (err) {
if (err) return handleError(err);
// we saved, now $shift works again
shifted = doc.array.$shift();
console.log(shifted ); // 2
console.log(doc.array); // [3]
})
MongooseArray.prototype.addToSet()
パラメータ
[...args]
«任意»
戻り値
- «配列» 追加された値
まだ存在しない場合、配列に値を追加します。
例
console.log(doc.array) // [2,3,4]
const added = doc.array.addToSet(4,5);
console.log(doc.array) // [2,3,4,5]
console.log(added) // [5]
MongooseArray.prototype.includes()
パラメータ
obj
«オブジェクト» チェックするアイテムfromIndex
«数値»
戻り値
- «真偽値»
`obj`が配列に含まれているかどうかを返します。
MongooseArray.prototype.indexOf()
パラメータ
obj
«オブジェクト» 検索するアイテムfromIndex
«数値»
戻り値
- «数値»
`obj`のインデックスを返します。見つからない場合は`-1`を返します。
MongooseArray.prototype.inspect()
console.logのヘルパー
MongooseArray.prototype.nonAtomicPush()
パラメータ
[...args]
«任意»
非アトミックに配列にアイテムを追加します。
注記
配列全体が変更されたとマークされます。保存すると、`$set`操作として保存され、オブジェクトを取得してから保存するまでの間に発生した変更が上書きされる可能性があります。
MongooseArray.prototype.pop()
参照
`Array#pop`を適切な変更追跡でラップします。
注記
配列全体が変更されたとマークされ、オブジェクトを取得してから保存するまでの間に発生した変更が上書きされる可能性があるため、`$set`にすべてを渡します。
MongooseArray.prototype.pull()
パラメータ
[...args]
«任意»
参照
アトミックに配列からアイテムをプル(削除)します。等価性は、提供された値を埋め込みドキュメントにキャストし、`Document.equals()`関数を使用して比較することで決定されます。
例
doc.array.pull(ObjectId)
doc.array.pull({ _id: 'someId' })
doc.array.pull(36)
doc.array.pull('tag 1', 'tag 2')
サブドキュメント配列からドキュメントを削除するには、一致する`_id`を持つオブジェクトを渡すことができます。
doc.subdocs.push({ _id: 4815162342 })
doc.subdocs.pull({ _id: 4815162342 }) // removed
または、_idを直接渡して、mongooseに処理させることもできます。
doc.subdocs.push({ _id: 4815162342 })
doc.subdocs.pull(4815162342); // works
最初のpull呼び出しはデータベースでアトミック操作を実行します。ドキュメントを保存せずにpullを繰り返し呼び出すと、配列全体に`$set`操作が使用され、その間にデータベースで発生した可能性のある変更が上書きされます。
MongooseArray.prototype.push()
パラメータ
[...args]
«オブジェクト»
`Array#push`を適切な変更追跡でラップします。
例
const schema = Schema({ nums: [Number] });
const Model = mongoose.model('Test', schema);
const doc = await Model.create({ nums: [3, 4] });
doc.nums.push(5); // Add 5 to the end of the array
await doc.save();
// You can also pass an object with `$each` as the
// first parameter to use MongoDB's `$position`
doc.nums.push({
$each: [1, 2],
$position: 0
});
doc.nums; // [1, 2, 3, 4, 5]
MongooseArray.prototype.remove()
参照
pullのエイリアス
MongooseArray.prototype.set()
戻り値
- «配列» this
キャストされた`val`をインデックス`i`に設定し、配列が変更されたとマークします。
例
// given documents based on the following
const Doc = mongoose.model('Doc', new Schema({ array: [Number] }));
const doc = new Doc({ array: [2,3,4] })
console.log(doc.array) // [2,3,4]
doc.array.set(1,"5");
console.log(doc.array); // [2,5,4] // properly cast to number
doc.save() // the change is saved
// VS not using array#set
doc.array[1] = "5";
console.log(doc.array); // [2,"5",4] // no casting
doc.save() // change is not saved
MongooseArray.prototype.shift()
`Array#shift`を適切な変更追跡でラップします。
例
doc.array = [2,3];
const res = doc.array.shift();
console.log(res) // 2
console.log(doc.array) // [3]
注記
配列全体が変更されたとマークされます。保存すると、`$set`操作として保存され、オブジェクトを取得してから保存するまでの間に発生した変更が上書きされる可能性があります。
MongooseArray.prototype.sort()
参照
`Array#sort`を適切な変更追跡でラップします。
注記
配列全体が変更されたとマークされます。保存すると、`$set`操作として保存され、オブジェクトを取得してから保存するまでの間に発生した変更が上書きされる可能性があります。
MongooseArray.prototype.splice()
参照
`Array#splice`を適切な変更追跡とキャスティングでラップします。
注記
配列全体が変更されたとマークされます。保存すると、`$set`操作として保存され、オブジェクトを取得してから保存するまでの間に発生した変更が上書きされる可能性があります。
MongooseArray.prototype.toObject()
パラメータ
options
«オブジェクト»
戻り値
- «配列»
ネイティブのjs配列を返します。
MongooseArray.prototype.unshift()
`Array#unshift`を適切な変更追跡でラップします。
注記
配列全体が変更されたとマークされます。保存すると、`$set`操作として保存され、オブジェクトを取得してから保存するまでの間に発生した変更が上書きされる可能性があります。