社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

GraphQL和MongoDB-“Cast to Array failed”和Array[duplicate]

av0000 • 5 年前 • 1169 次点击  

var Class = mongoose.model('Class', {className: String, marks: [{type: Number}], grades: [{type: Number}]});
var User = mongoose.model('User', {email: String, classes: [Class] });


//Lets create a new user
var class1 = new Class({className: 'aaa', marks: [72, 88, 63], grades: [30, 40, 30]});
var user1 = new User({email: 'aaa@some.com', classes: [class1]});

保存 class1 似乎工作正常,但当我检查mongodb时,显示:

{ 
  "_id" : ObjectId("someId"), 
  "className" : "TEST1234", 
  "grades" : [ 30, 40, 30 ], 
  "marks" : [ 72, 88, 63 ], 
  "__v" : 0 
}

"__v : 0" 是吗?

ValidationError:CastError:Cast to Array失败,值为“{marks:[72,88,63], _身份证:某某, 类名:路径“classes”处的“TEST1234”}

这个错误到底是什么意思?为什么它要向数组中投射任何东西?不应该 classes: [Class] class 是吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55255
 
1169 次点击  
文章 [ 7 ]  |  最新文章 5 年前
Grégory NEUT
Reply   •   1 楼
Grégory NEUT    5 年前

我使用Mongoose5.7.0+使用双嵌套模式时遇到了类似的问题。

type 但是猫鼬的确认错误。

https://github.com/Automattic/mongoose/issues/8472

Schema.Types.Mixed 对于子模式

J. Pichardo
Reply   •   2 楼
J. Pichardo    8 年前

现在Mongoose支持子文档,这是嵌套数组的文档化方法,

var arraySchema = new Schema({
    property: String
});

var objectSchema = new Schema({
    arrays: [arraySchema]
});

来源

http://mongoosejs.com/docs/schematypes.html

Sergio Rodrigues
Reply   •   3 楼
Sergio Rodrigues    8 年前

默认情况下,如果架构中有一个键为“type”的对象,mongoose会将其解释为类型声明。

// Mongoose interprets this as 'loc is a String'
var schema = new Schema({ loc: { type: String, coordinates: [Number] } });

更改类型键:

var schema = new Schema({
  // Mongoose interpets this as 'loc is an object with 2 keys, type and coordinates'
  loc: { type: String, coordinates: [Number] },
  // Mongoose interprets this as 'name is a String'
  name: { $type: String }
}, { typeKey: '$type' }); // A '$type' key means this object is a type declaration

链接: http://mongoosejs.com/docs/guide.html#typeKey

Furkan Başaran
Reply   •   4 楼
Furkan Başaran    9 年前

您的模型定义不正确,应该如下修复。

// var Schema = mongoose.Schema;
var User = mongoose.model('User',{ 
  email: String, 
  classes: [ {type: Schema.Types.ObjectID, ref: 'Class'}] 
});

var Class1 = new Class({/*yourDataWillBeHere*/})

Class1.save(function(err, classData) {
   var User1 = new User({/*YourDataWillBeHere*/})
   User1.classes.push(classData._id);
   User1.save(function(err, userData) {
      //make something with userData object 
   })
})

populate() 这样地

User
.find()
.populate('classes')
.exec()
user2709641
Reply   •   5 楼
user2709641    9 年前

尝试将类定义更改为:

var classSchema = mongoose.Schema({className: String, marks: [{type: Number}], grades: [{type: Number}]});
var userSchema = mongoose.Schema({email: String, classes: [classSchema] });
var User = mongoose.model('User',userSchema);

这是必需的,因为mongoose无法在没有相关架构的情况下解析对象。现在,当您为内部类对象创建一个新模式并在主userSchema中引用它时,mongoose应该能够解析您的对象。

Aaron Moore
Reply   •   6 楼
Aaron Moore    7 年前

type: {type: String}
nopassport1 Vinicius Lima
Reply   •   7 楼
nopassport1 Vinicius Lima    5 年前

伙计,我在创建这样的模式时遇到了类似的问题:

QuestionnaireSchema = mongoose.Schema({
    formId: Number,
    name: String,
    questions: [
        {
            type: String,
            title: String,
            alternatives:[{
                label: String,
                value: "Mixed"
            }]
        }
    ]
});

我的错误是我使用“type”作为字段名,这是猫鼬的保留字。

我只是改变:

type: String,

formType: String,

这很管用。

见: https://github.com/Automattic/mongoose/issues/1760