gpt4 book ai didi

javascript - Mongoose 和 Schema.Types.Mixed 的问题

转载 作者:可可西里 更新时间:2023-11-01 10:43:43 26 4
gpt4 key购买 nike

我试图拥有一个完全混合的模型,但无法真正让它工作,这是完整的 javascript:

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

mongoose.connect('mongodb://localhost/whatever');

var SomeDocument = mongoose.model('SomeDocument', new Schema({ any: Schema.Types.Mixed }) );

var AnotherDocument = mongoose.model('AnotherDocument', new Schema({ any: Schema.Types.Mixed }, { strict: false } ) );

async.waterfall([
function(cb) {
var saveThis = {lets: "have", some: "fun"};
console.log("Trying to save:", saveThis);
SomeDocument.create(saveThis, function(err, savedDoc) {
console.log("Created SomeDocument: ", savedDoc);
cb(null, savedDoc._id.toString());
});
},
function(id, cb) {
SomeDocument.findOne({_id: id}, function(err, someDoc) {
console.log("Found SomeDocument", someDoc);
cb(null);
});
},
function(cb) {
AnotherDocument.create({maybe: "now", we: "can", have: "fun"}, function(err, savedDoc) {
console.log("Created AnotherDocument", savedDoc);
cb(null, savedDoc._id.toString());
});
},
function(id, cb) {
AnotherDocument.findOne({_id: id}, function(err, anotherDoc) {
console.log("Found AnotherDocument", anotherDoc);
console.log("Seems like progress, but what about: ", anotherDoc._id, anotherDoc.maybe, anotherDoc.we, anotherDoc.have);
console.log("need moar brains");
cb(null);
});
},
function(cb) {
mongoose.disconnect();
}
]);

SomeDocument 或 AnotherDocument 都没有像我预期的那样工作。第一个甚至不保存额外的字段,另一个不让我读取对象属性...

以上代码输出:

$ node test.js
Trying to save: { lets: 'have', some: 'fun' }
Created SomeDocument: { __v: 0, _id: 53be7279c90a4def0d000001 }
Found SomeDocument { _id: 53be7279c90a4def0d000001, __v: 0 }
Created AnotherDocument { __v: 0,
maybe: 'now',
we: 'can',
have: 'fun',
_id: 53be7279c90a4def0d000002 }
Found AnotherDocument { maybe: 'now',
we: 'can',
have: 'fun',
_id: 53be7279c90a4def0d000002,
__v: 0 }
Seems like progress, but what about: 53be7279c90a4def0d000002 undefined undefined undefined
need moar brains

这是一个错误还是我遗漏了什么?

主要问题是,尽管 console.log 实际上似乎在保存文档时输出文档,但当我尝试访问 anotherDoc.maybe 时 - 它突然 undefined

版本:

mongoose@3.6.20 node_modules/mongoose
├── regexp-clone@0.0.1
├── sliced@0.0.5
├── muri@0.3.1
├── hooks@0.2.1
├── mpath@0.1.1
├── ms@0.1.0
├── mpromise@0.2.1 (sliced@0.0.4)
└── mongodb@1.3.19 (kerberos@0.0.3, bson@0.2.2)

$ node -v
v0.10.25

$ uname -a
Linux deployment 3.11.0-24-generic #42-Ubuntu SMP Fri Jul 4 21:19:31 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

最佳答案

您对“waterfall ”的用法实际上是不正确的。如果您希望这些变量在您的最终操作中可见,那么您需要传递所有这些变量。因此,每个连续的回调都需要“传递”您收到的变量。这些不只是自动“waterfall 式下降”到最低级别。

不过,这里是真正的问题。

如果您只想要一种完全无模式的方法,那么只需使用 {"strict": false } 即可。

var docSchema = new Schema({},{ "strict": false });
var Document = mongoose.model( "Document", documentSchema );

这不限制领域,基本上一切正常。 “混合”实际上仅适用于可能因字段和类型而异的字段定义。

默认的 _id 在这种情况下保留为 ObjectId,但您似乎并没有覆盖它。


再看题,漏了"strict": falsefrom the documentation .否则“定义”字段的访问器不存在。您需要使用通用的 .set().get():

var async = require("async"),
mongoose = require("mongoose"),
Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/test');


var mixSchema = new Schema({},{ "strict": false });

var Mix = mongoose.model( "Mix", mixSchema );

var report = function(label,data) {
console.log(
"%s\n%s\n",
label,
JSON.stringify( data, undefined, 4 )
);
};

async.waterfall([

function(callback) {
Mix.create({ "lets": "have", "some": "fun" },function(err,doc) {
if (err) throw err;
report("Created",doc);
callback(null,doc._id);
});
},

function(id,callback) {
Mix.findById(id,function(err,doc) {
if (err) throw err;
report("Found",doc);
callback(null);
});
},

function(callback) {
Mix.create({
"maybe": "now", "we": "can", "have": "fun"
},function(err,doc) {
if (err) throw err;
report("Created",doc);
callback(null,doc._id);
});

},

function(id,callback) {
Mix.findById(id,function(err,doc) {
if (err) throw err;
report("Found",doc);
console.log(
"But does not have built in accessors when strict:false. So...\n"
);

console.log(
"\"maybe\": \"%s\" \"we\": \"%s\" \"have\": \"%s\"\n",
doc.get("maybe"), doc.get("we"), doc.get("have")
);

// Or get raw values
console.log("In the raw!\n");
var raw = doc.toObject();
for ( var k in raw ) {
console.log( "\"%s\": \"%s\"", k, raw[k] );
}

console.log("\nAnd again\n");
console.log(
"\"maybe\": \"%s\" \"we\": \"%s\" \"have\": \"%s\"\n",
raw.maybe, raw.we, raw.have
);

callback();
});
},

],function(err) {
console.log("done");
process.exit();
});

如前所述,_id 访问器仍然存在,默认情况下它在那里,除非您再次将其关闭。对于其他人,尽管您使用 .get() 方法。如果您通常使用 MongoDB update operators,那么更新通常不是什么大问题。 .但是对于 mongoose 对象的任何 JavaScript 代码操作,您必须调用 .set()

输出:

Created
{
"__v": 0,
"lets": "have",
"some": "fun",
"_id": "53bf6f09ac1add4b3b2386b9"
}

Found
{
"_id": "53bf6f09ac1add4b3b2386b9",
"lets": "have",
"some": "fun",
"__v": 0
}

Created
{
"__v": 0,
"maybe": "now",
"we": "can",
"have": "fun",
"_id": "53bf6f09ac1add4b3b2386ba"
}

Found
{
"_id": "53bf6f09ac1add4b3b2386ba",
"maybe": "now",
"we": "can",
"have": "fun",
"__v": 0
}

But does not have built in accessors when strict:false. So...

"maybe": "now" "we": "can" "have": "fun"

In the raw!

"_id": "53bf6f09ac1add4b3b2386ba"
"maybe": "now"
"we": "can"
"have": "fun"
"__v": "0"

And again

"maybe": "now" "we": "can" "have": "fun"

done

关于javascript - Mongoose 和 Schema.Types.Mixed 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24674909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com