【问题标题】:Unable insert more than 1 entry in MongoDB无法在 MongoDB 中插入超过 1 个条目
【发布时间】:2017-10-21 14:31:32
【问题描述】:

这是我的架构。 user_id 和 other_id 应该是唯一的(复合)。

var mongoose = require("mongoose");
var uniqueValidator = require('mongoose-unique-validator');

var Schema = mongoose.Schema;

var FriendshipSchema = new Schema({

  user_id: {
    type: String,
    default: "",  
    trim: true,
unique:true,
  },
  other_id: {
    type: String,
    default: "",
unique:true,
  },

  status: {
    type: String,
    index: true,
    default: "none",
  },

});
FriendshipSchema.plugin(uniqueValidator);

module.exports =  mongoose.model('Friendship', FriendshipSchema)

这是我的服务器端代码。使用 Mongoose 非常简单的插入。

app.post('/api/user/friendrequest', function(req, res){
var friendship = new Friendship(req.body);
console.log(req.body);

 Friendship.find({}, function (err, docs) {
        if (docs.length){
console.log('abac');
        }else{
            friendship.save(function(err){

            if (err)
            {
             console.log(err)
            }
            });
        }
    });
    });

我在控制台中收到此响应,但在 MongoDB 中保存的条目不超过 1 个。我也删除了索引,但它仍然无法正常工作。顺便说一句,“user_id”在另一个集合中是唯一的。当我登录console.log(err)时,我也没有收到任何错误。

{ user_id: 'google-oauth2|117175967810648931400',
  status: 'pending',
  other_id: 'facebook|10209430751350509' }
abac

这里是友谊集合的索引。

 db.friendships.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "kola.friendships"
        }
]

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    你想要的是字段的“组合”在这里是“唯一的”,而不是被单独对待。

    这意味着您的架构应该这样定义:

    var FriendshipSchema = new Schema({
    
      user_id: {
        type: String,
        default: "",  
        trim: true,
      },
      other_id: {
        type: String,
        default: "",
      },
    
      status: {
        type: String,
        index: true,
        default: "none",
      },
    
    });
    
    // Instead define the schema level index here
    FriendshipShema.index({ "user_id": 1, "other_id": 1 },{ "unique": true });
    
    module.exports =  mongoose.model('Friendship', FriendshipSchema);
    

    最好的部分是你不需要任何插件来支持你想做的事情。

    请确保您在集合上运行.dropIndexes(),以消除任何会干扰正确操作的单独“唯一”索引。

    有关详细信息,另请参阅核心文档中的 .createindex()"Unique Indexes"

    【讨论】:

    • 我删除了表,删除了除 id 之外的所有索引,但我仍然只能添加 1 个条目,但仍然没有错误。但是在某个阶段,我得到了这个'驱动程序:true,代码:11000,索引:0,errmsg:'E11000重复密钥错误集合:kola.friendships index:user_id_1_other_id_1 dup key:{:“google-oauth2|117175967810648931400”,: “google-oauth2|109598513284156650220”}',getOperation:[Function],toJSON:[Function],toString:[Function]}'
    • @AlamgirQazi 是的,这是设计使然。您只能拥有 一个 字段组合。你在期待什么?
    • 我有 5 个用户 A、B、C、D、E,每个用户都有不同的 user_id。对于一个记录,我想要 AB,对于一个我想要 AC 等。因为每个人都有唯一的密钥,我应该能够添加它们吧?
    • @AlamgirQazi 这就是我在这里强制执行的内容,您可以组合 AB AC AE 等,但决不能组合其他组合,例如 BA,因为这种关系已经用唯一条目定义。即 AB == BA。请注意,您的代码上的“更正”是您无法多次定义 A。将唯一约束设置为 :"combination" 可以解决此问题。
    • 谢谢!是的。这正是我想要的,现在我知道需要索引,但我仍然只能添加一条记录。表示一次只存在一个 A。不知道我做错了什么。也许是因为 user_id 在另一个集合中是唯一的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多