可能的解決方法是使用Schema.Types.Mixed
。比方說,你需要創建block
對象的2x2的陣列(我沒有測試此代碼):
var mongoose = require('mongoose')
, Schema = mongoose.Schema,
, Mixed = Schema.Types.Mixed;
var block = new Schema({
type: Number,
colour: String
});
var gridSchema = new Schema({
arr: { type: Mixed, default: [] }
});
var YourGrid = db.model('YourGrid', gridSchema); // battleship is 2D, right?
現在,讓我們假設你在這裏創建4個「塊」的對象(塊1,塊2,塊3,塊4 ),那麼你可以做:
var yourGrid = new YourGrid;
yourGrid.arr.push([block1, block2]);
// now you have to tell mongoose that the value has changed
// because with Mixed types it's not done automatically...
yourGrid.markModified('arr');
yourGrid.save();
然後,在接下來的一個2點的對象,block3
和block4
相同。