如何在MongoDB中不创建集合的情况下创建Mongoose模型?

jus*_*tin 5 mongoose

我只想versioneditems在MongoDB中有一个集合,但是我需要同时注册VersionedItem模型和ItemPatch模型,因为我需要创建ItemPatches来填充VersionedItem

将没有单独的ItemPatch文档(它们嵌入在中VersionedItem)。除了在MongoDB中创建了一个额外的集合外,以下代码可以正常工作:

src / models / versionedItemFactory.js

const VersionedItemSchema = require('../schemas/VersionedItem');

module.exports = (db) => {
  var VersionedItemModel = db.model('VersionedItem', VersionedItemSchema);

  return VersionedItemModel;
};
Run Code Online (Sandbox Code Playgroud)

src / models / itemPatchFactory.js

const ItemPatchSchema = require('../schemas/ItemPatch');

module.exports = (db) => {
  var ItemPatchModel = db.model('ItemPatch', ItemPatchSchema);

  return ItemPatchModel;
};
Run Code Online (Sandbox Code Playgroud)

src / schemas / util / asPatch.js

var mongoose = require('mongoose');

module.exports = function _asPatch(schema) {

  return new mongoose.Schema({
    createdAt: { type: Date, default: Date.now },
    jsonPatch: {
      op: { type: String, default: 'add' },
      path: { type: String, default: '' },
      value: { type: schema }
    }
  });
};
Run Code Online (Sandbox Code Playgroud)

src / schemas / Item.js

var mongoose = require('mongoose');

module.exports = new mongoose.Schema({
  title: { type: String, index: true },
  content: { type: String },
  type: { type: String, default: 'txt' }
}, { _id: false });
Run Code Online (Sandbox Code Playgroud)

src / schemas / ItemPatch.js

var asPatch = require('./util/asPatch');
var ItemSchema = require('./Item');

module.exports = asPatch(ItemSchema);
Run Code Online (Sandbox Code Playgroud)

src / schemas / VersionedItem.js

var mongoose = require('mongoose');
var ItemPatchSchema = require('./ItemPatch');

module.exports = new mongoose.Schema({
  createdAt: { type: Date, default: Date.now },
  patches: [
    {
      createdAt: { type: Date, default: Date.now },
      jsonPatch: { type: ItemPatchSchema }
    }
  ]
});
Run Code Online (Sandbox Code Playgroud)

然后像这样注册:

  db.once('open', function() {
    require('./models/itemPatchFactory')(db);
    require('./models/versionedItemFactory')(db);
  });
Run Code Online (Sandbox Code Playgroud)

我需要通过注册ItemPatch模型,itemPatchFactory因为我希望能够像这样填充版本化的项目:

var itemPatch = new db.models.ItemPatch({
  jsonPatch: {
    op: 'add',
    path: '',
    value: { 
      title: 'This is a title',
      content: 'This is content',
      type: 'txt'
    }
  }
});

var itemPatch2 = new db.models.ItemPatch({
  jsonPatch: {
    value: { 
      title: 'This is a title 2',
      content: 'This is content 2'
    }
  }
});

var versionedSomething = new db.models.VersionedItem();
versionedSomething.patches.push(itemPatch);
versionedSomething.patches.push(itemPatch2);

versionedSomething.save(function (err, result) {
  if (err) throw err;

  console.log('result:', result);
});
Run Code Online (Sandbox Code Playgroud)

这样可以成功创建包含2个补丁的版本控制项,但是itempatches在MongoDB中创建了一个(空)集合,我想避免这种情况。

Dov*_*erg 2

如果没有相应的集合,您就无法创建 a Model,但我认为您实际上不需要这样做才能做您想做的事情。

您可以简单地为子集合创建一个 javascript 对象并将其推送到父集合。请参阅 Mongoose 文档中的这段代码 ( https://mongoosejs.com/docs/subdocs.html )

var Parent = mongoose.model('Parent');
var parent = new Parent;

// create a comment
parent.children.push({ name: 'Liesl' });
var subdoc = parent.children[0];
console.log(subdoc) // { _id: '501d86090d371bab2c0341c5', name: 'Liesl' }
subdoc.isNew; // true

parent.save(function (err) {
  if (err) return handleError(err)
  console.log('Success!');
});
Run Code Online (Sandbox Code Playgroud)

但是,您可以Schema为子文档创建一个。这将使您在从集合读取/写入时强制执行结构:

var childSchema = new Schema({ name: 'string' });

var parentSchema = new Schema({
  // Array of subdocuments
  children: [childSchema],
  // Single nested subdocuments. Caveat: single nested subdocs only work
  // in mongoose >= 4.2.0
  child: childSchema
});
Run Code Online (Sandbox Code Playgroud)