[英]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
還使用此鏈接(第一種方法)以正常方式嘗試過,仍然無法使用@定義鍵: http : //blog.modulus.io/mongodb-tutorial
我的目的是使用JSON-LD格式創建文檔,該格式要求在鍵中使用@符號。 如何做到這一點? 這是我尋找解決方案的類似鏈接:
帶有mongodb點符號的變量
語法錯誤意外的令牌ILLEGAL Mongo Console
如何使用帶有動態鍵的貓鼬模型架構?
如何通過Node.js中的Mongoose使用dot(。)進行查詢以及如何添加空數組
使用自定義鍵/值在Mongoose / Handlebars中創建Schema對象
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();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.