使用 mongoose 插入 MongoDB 时在键中使用“@”
Using "@" in key when inserting to MongoDB with mongoose
当使用 Node.js 插入到 MongoDB 时,Mongoose 模式不允许我使用 @ 登录密钥。例如:
var blogSchema = new Schema({
@context : Object //error illegal token
@id : String // illegal token
}, {strict: false});
我试过使用 unicode 字符的密钥,如下所示:
"\u0040context" = Object // ignored unicode, inserted as context
"\x40context" = Object // ignored unicode, inserted as context
\x40context = Object // illegal token
也尝试过使用这个link(第一种方式)的正常方式,仍然无法用@定义键:
http://blog.modulus.io/mongodb-tutorial
我的目的是使用 JSON-LD 格式创建文档,这需要在键中使用 @ 符号。如何做到这一点?这是我寻找解决方案的类似 links:
variable with mongodb dotnotation
Syntax error Unexpected token ILLEGAL Mongo Console
How to do a query using dot( . ) through Mongoose in Node.js and How to add an empty array
Create a Schema object in Mongoose/Handlebars with custom keys/values
http://mongoosejs.com/docs/schematypes.html
您可以在引号之间直接使用 @
例如 "@field"
:
"use strict";
var mongoose = require('./node_modules/mongoose');
var Schema = mongoose.Schema;
var db = mongoose.connection;
var itemSchema = new Schema({
"@field": {
type: String,
required: true
},
"@field2": {
type: String,
required: true
}
});
var Items = mongoose.model('Items', itemSchema);
var db = mongoose.connect('localhost', 'testDB');
Items.create({ "@field": "value", "@field2": "value" }, function(err, doc) {
console.log("created");
if (err)
console.log(err);
else {
console.log(doc);
}
db.disconnect();
});
当使用 Node.js 插入到 MongoDB 时,Mongoose 模式不允许我使用 @ 登录密钥。例如:
var blogSchema = new Schema({
@context : Object //error illegal token
@id : String // illegal token
}, {strict: false});
我试过使用 unicode 字符的密钥,如下所示:
"\u0040context" = Object // ignored unicode, inserted as context
"\x40context" = Object // ignored unicode, inserted as context
\x40context = Object // illegal token
也尝试过使用这个link(第一种方式)的正常方式,仍然无法用@定义键: http://blog.modulus.io/mongodb-tutorial
我的目的是使用 JSON-LD 格式创建文档,这需要在键中使用 @ 符号。如何做到这一点?这是我寻找解决方案的类似 links:
variable with mongodb dotnotation
Syntax error Unexpected token ILLEGAL Mongo Console
How to do a query using dot( . ) through Mongoose in Node.js and How to add an empty array
Create a Schema object in Mongoose/Handlebars with custom keys/values
http://mongoosejs.com/docs/schematypes.html
您可以在引号之间直接使用 @
例如 "@field"
:
"use strict";
var mongoose = require('./node_modules/mongoose');
var Schema = mongoose.Schema;
var db = mongoose.connection;
var itemSchema = new Schema({
"@field": {
type: String,
required: true
},
"@field2": {
type: String,
required: true
}
});
var Items = mongoose.model('Items', itemSchema);
var db = mongoose.connect('localhost', 'testDB');
Items.create({ "@field": "value", "@field2": "value" }, function(err, doc) {
console.log("created");
if (err)
console.log(err);
else {
console.log(doc);
}
db.disconnect();
});