2016-10-29 2 views
0

몽구스 스키마는 Node.js를 사용하여 MongoDB에 삽입 할 때 @ sign in key를 사용할 수 없습니다. 예를 들어 : http://blog.modulus.io/mongodb-tutorial몽구스로 MongoDB에 삽입 할 때 키에 "@"사용하기

: 여전히 @와 키를 정의 할 수 없습니다, 또한이 링크 (첫 번째 방법)을 사용하여 일반적인 방법으로 시도

"\u0040context" = Object // ignored unicode, inserted as context 
"\x40context" = Object // ignored unicode, inserted as context 
\x40context = Object // illegal token 

:

var blogSchema = new Schema({ 
@context : Object //error illegal token 
@id : String // illegal token 


}, {strict: false}); 

나는이 같은 유니 코드 문자 키를 시도

나의 목적은 키에 @ 기호를 사용해야하는 JSON-LD 형식을 사용하여 문서를 만드는 것입니다. 이 일을 성취하는 방법? 여기에 내가 솔루션을 보았다 유사한 링크입니다
variable with mongodb dotnotation
Syntax error Unexpected token 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
Create a Schema object in Mongoose/Handlebars with custom keys/values
http://mongoosejs.com/docs/schematypes.html

답변

2

당신은 "@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(); 
}); 
+0

오 시간과 세심한 답변을 주셔서 감사합니다. 매력처럼 작동했습니다. – nihirus