Mongoose无_id保存数据

6
我在使用Node.js应用程序的mongoose。我不想记录中有_id字段。我正在使用以下代码来保存没有_id字段的记录。但是它会报错。

在保存之前,文档必须具有_id

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var PlayerSchema = new Schema({
    player_id :  { type: Number },
    player_name : { type: String },
    player_age  : { type: Number },
    player_country : { type: String }
}
, { _id: false }
);

var Player =  mongoose.model('Player', PlayerSchema );

        var athlete = new Player();
        athlete.player_id = 1;
        athlete.player_name = "Vicks";
        athlete.player_age  = 20;
        athlete.player_country = "UK";

        athlete.save(function(err) {
            if (err){
                console.log("Error saving in PlayerSchema"+ err);
            }
        });

我正在使用mongoose版本3.8.14。

你真的需要保存没有 _id 的数据,还是你只是不想自己声明它? - ffflabs
我不想在记录中保存 _id。我正在生成自己的唯一 player_id。 - mabc224
1
你可以在player_id字段上定义一个唯一索引以确保唯一性,或者将player_id重命名为_id。无论哪种方式,在mongoDB中,文档都必须有一个_id,只是将其创建委托给数据库层。你尝试过在模式定义中跳过{ _id: false }吗? - ffflabs
3个回答

3

很遗憾,您无法跳过为文档设置主键,但是您可以覆盖主键内容,您可以为每个文档定义自己的主键。

尝试使用以下模式。

var PlayerSchema = new mongoose.Schema({
_id :  { type: Number },
player_name : { type: String },
player_age  : { type: Number },
player_country : { type: String },

我已将您的player_id替换为_id。现在,您可以控制文档的主键,系统不会为您生成该键。

还有一些插件可以为您的主键执行autoincremet操作。 https://github.com/chevex-archived/mongoose-auto-increment。 您也可以尝试使用这些插件。

此外,关于您遇到的错误: 任何文档都是一个对象,并且应该被包装在curly brackets中。您不能在同一文档中定义两个独立的对象。因此,您会收到此错误。


1
这是由Mongo自动生成的;覆盖类型为mongoose.Types.ObjectId()的属性_id并将其转换为数字将在未来导致许多错误。 - Oliver Dixon
请问您能否解释一下,我们采用这种方法可能会遇到哪些类型的错误? - Minkesh Jain
@MinkeshJain 关键冲突,最好让数据库自己处理。 - ruX

3

参考Github中类似问题的答案:

The _id option exists to prevent one being created and assigned to every sub-document within your document.

So, it's not for this:

var Schema = mongoose.Schema,
ThingSchema = new Schema({    
   name: String,  
   categories: [{         
      label: {            
         type: String,            
         uppercase: true      
      },      
      value: String
   }]
}, {_id: false});
//   ^^^^^^^^^
// this won't work 

It's for this:

var Schema = mongoose.Schema,
ThingSchema = new Schema({
    name: String,
    categories: [{
       label: {
          type: String,
          uppercase: true
       },
       value: String,
       _id: false // <--- this works
    }]
}); 

Took me a while but, after running into the same problem, I found the documentation here.


0

不要写这个

{
    player_id :  { type: Number },
    player_name : { type: String },
...}

写下这个:-
{
    _id :  { type: Number },
    _name : { type: String },
    _age  : { type: Number },
    _country : { type: String }
}

因此,_id 将变为 players_id

然后进一步编写如下代码:

var athlete = new Player();
        athlete._id = 1;
        athlete._name = "Vicks";
        athlete._age  = 20;
        athlete._country = "UK";

        athlete.save(function(err) {
            if (err){
                console.log("Error saving in PlayerSchema"+ err);
            }
        });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接