[英]Using “@” in key when inserting to MongoDB with mongoose
Mongoose schema won't let me use @ sign in key when inserting to MongoDB with using Node.js. 当使用Node.js插入MongoDB时,Mongoose模式不会让我使用@登录密钥。 For instance:
例如:
var blogSchema = new Schema({
@context : Object //error illegal token
@id : String // illegal token
}, {strict: false});
I tried key with unicode characters like this one: 我尝试了像这样的Unicode字符的键:
"\u0040context" = Object // ignored unicode, inserted as context
"\x40context" = Object // ignored unicode, inserted as context
\x40context = Object // illegal token
Also tried with normal way using this link(first way), still cannot define key with @: http://blog.modulus.io/mongodb-tutorial 还使用此链接(第一种方法)以正常方式尝试过,仍然无法使用@定义键: http : //blog.modulus.io/mongodb-tutorial
My purpose is to create document with using JSON-LD format which requires of using @ symbol in key. 我的目的是使用JSON-LD格式创建文档,该格式要求在键中使用@符号。 How to accomplish this?
如何做到这一点? Here are the similar links I have looked for solution:
这是我寻找解决方案的类似链接:
variable with mongodb dotnotation 带有mongodb点符号的变量
Syntax error Unexpected token ILLEGAL Mongo Console 语法错误意外的令牌ILLEGAL Mongo Console
How to use mongoose model schema with dynamic keys? 如何使用带有动态键的猫鼬模型架构?
How to do a query using dot( . ) through Mongoose in Node.js and How to add an empty array 如何通过Node.js中的Mongoose使用dot(。)进行查询以及如何添加空数组
Create a Schema object in Mongoose/Handlebars with custom keys/values 使用自定义键/值在Mongoose / Handlebars中创建Schema对象
http://mongoosejs.com/docs/schematypes.html http://mongoosejs.com/docs/schematypes.html
You can use directly @
between quotes such as "@field"
: 您可以在引号之间直接使用
@
,例如"@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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.