Javascript 猫鼬通过操纵数据表现得很怪异
我挠了差不多四个小时的头。如您所见,我刚刚用Javascript 猫鼬通过操纵数据表现得很怪异,javascript,node.js,mongodb,object,mongoose,Javascript,Node.js,Mongodb,Object,Mongoose,我挠了差不多四个小时的头。如您所见,我刚刚用Mongoose查询了MongoDB,我得到了结果,但它似乎在某种程度上数据被操纵了!我不确定是缓冲区溢出还是下溢还是其他原因。 以下是我的简化nodejs代码: let mongoose = require("mongoose"); mongoose.connect("mongodb://localhost:27017/shop", {}).then(() => { let schema = ne
Mongoose
查询了MongoDB
,我得到了结果,但它似乎在某种程度上数据
被操纵了!我不确定是缓冲区溢出
还是下溢
还是其他原因。
以下是我的简化nodejs代码:
let mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/shop", {}).then(() => {
let schema = new mongoose.Schema({
categoryId: {
type: Number,
required: true,
},
products: {
shirt: {
type: String,
required: true,
},
glove: {
type: String,
required: true,
},
mitten: {
type: String,
required: true,
},
glasses: {
type: String,
required: true,
},
},
});
let Model = mongoose.model("Model", schema, "services_products");
Model.findOne({ categoryId: { $exists: false } }).then((data) => {
console.log("data: ", data);
console.log("products: ", typeof data.products, data.products);
console.log("products keys: ", Object.keys(data.products));
console.log("products values: ", Object.values(data.products));
});
});
以下是控制台日志:
(node:15151) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:15151) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
data: {
products: {
sunGlasess: 'MAIN_STORE',
normalGlasses: 'MAIN_STORE',
smokeGlasses: 'MAIN_STORE',
sportGlasses: 'MAIN_STORE'
},
_id: 5fe1d3ad223ac40b32038415
}
products: object {
sunGlasess: 'MAIN_STORE',
normalGlasses: 'MAIN_STORE',
smokeGlasses: 'MAIN_STORE',
sportGlasses: 'MAIN_STORE'
}
products keys: [ '$init', 'shirt', 'glove', 'mitten', 'glasses' ]
products values: [ true, undefined, undefined, undefined, undefined ]
products
对象与products键
和products值
不匹配
注意:实际上,有一个文档将衬衫、手套、手套、眼镜作为其产品的键,但它们都有价值
问题是模式
。我忘了在模式的产品
字段中添加太阳镜、普通眼镜、烟镜、运动眼镜
。我修改了我的模式:
let schema = new mongoose.Schema({
categoryId: {
type: Number,
required: true,
},
products: mongoose.Schema.Types.Mixed
});
现在,产品可以是任何对象。您尝试过先执行data.toObject()时会发生什么吗?