mongoose Mongo模式,具有唯一值的字符串数组

pes8fvy9  于 2023-01-05  发布在  Go
关注(0)|答案(5)|浏览(125)

我正在为一个mongo文档创建模式,除了防止非对象数组中的重复之外,我可以做任何事情。
我知道addToSet,但我指的是Mongo模式。
我不想使用$addToSet检查Update,而是希望它成为模式验证的一部分。
示例如下。

let sampleSchema = {
   name: { type: 'String', unique: true },
   tags: [{ type: 'String', unique: true }]
}

上面的代码片段防止name有重复的值。它允许标记存储为字符串数组。
但是..我不能限制数组为唯一字符串。

{ name: 'fail scenario', tags: ['bad', 'bad', 'array']}

我能够插入此记录,这应该是一个失败的场景。

pwuypxnk

pwuypxnk1#

const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const _ = require('underscore');

let sampleSchema = new mongoose.Schema({
  name: {
    type: 'String',
    unique: true
  },
  tags: [{
    type: 'String'
  }]
})
sampleSchema.pre('save', function (next) {
  this.tags = _.uniq(this.tags);
  next();
});
const Sample = mongoose.model('sample', sampleSchema, 'samples');

router.post('/sample', function (req, res, next) {
  const sample = new Sample(req.body);

  sample.save()
    .then((sample) => {
      return res.send(sample);
    })
    .catch(err => {
      return res.status(500).send(err.message);
    })
});
ws51t4hk

ws51t4hk2#

我得出的结论是,这是不可能通过 Mongoose 模式。
JSON模式是这样完成的。

let schema = {
   name: { type: 'string' }
   tags: { 
      type: 'array',
      items: { type: 'string', uniqueItems: true }
   }
}

在创建Mongo文档之前,我将使用JSON模式进行验证。

dvtswwa3

dvtswwa33#

这个方法建立在Med的答案上,处理引用,并且完全在方案验证中完成。

let sampleSchema = new mongoose.Schema({
    strings: [{type: 'String'}],
    references: [{type: mongoose.Schema.Types.ObjectId, ref: 'Reference'],
});

sampleSchema.pre('save', function (next) {

    let sample = this;

    sample.strings = _.uniq(sample.strings, function(i) {return (i._id) ? i._id.toString() : i;});
    sample.references = _.uniq(sample.references, function(i) {return (i._id) ? i._id.toString() : i;});

    return next();

});
nzk0hqpo

nzk0hqpo4#

我有点晚了,但也许这对将来的人有帮助。

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  name: {
    type: String,
  },
  reference: {
    type: [mongoose.Schema.Types.ObjectId],
    ref: 'SomeOtherSchema',
    // Add a custom validator.
    validate: {
      // The actual validator function goes here.
      // "arr" will be the value that's being validated (so an array of 
      // mongoose new ObjectId statements, in this case).
      validator: arr => {
        // Convert all of the items in the array "arr", to their string
        // representations.
        // Then, use those strings to create a Set (which only stores unique
        // values).
        const s = new Set(arr.map(String));
        // Compare the Set and Array's sizes, to see if there were any
        // duplicates. If they're not equal, there was a duplicate, and
        // validation will fail.
        return s.size === arr.length;
      },
      // Provide a more meaningful error message.
      message: p => `The values provided for '${ p.path }', ` +
                    `[${ p.value }], contains duplicates.`,
    }
  },
});

上面的注解代码应该是非常不言自明的。

lsmepo6l

lsmepo6l5#

在MongoDB的更新版本中,当且仅当新值与数组中的元素相比是唯一的时,才可以使用$addToSet追加到数组中。
参考资料如下:https://www.mongodb.com/docs/manual/reference/operator/update/addToSet/
下面是一个例子:

const SampleSchema = new mongoose.Schema({
    tags: [String]
});

const Sample = mongoose.model('Sample', SampleSchema);

// append to array only if value is unique
Sample.findByIdAndUpdate({_id: 1, {$addToSet: {tags: "New Tag"}}});

如果tags数组中还没有"新标签",这将有效地更新标签。否则,不执行任何操作。

相关问题