简体   繁体   English

用猫鼬插入MongoDB时在键中使用“ @”

[英]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.

相关问题 在不使用 mongoose/mongodb 的情况下填充 object 密钥 - Populate object key without using mongoose/mongodb 使用mongoose和node.js在mongoDB中插入文档时遇到问题 - Having trouble inserting documents in mongoDB using mongoose and node.js 使用mongoose将javascript数组插入mongodb时出错 - Error while inserting javascript array to mongodb using mongoose 使用Mongoose时出现MongoDb连接错误 - MongoDb Connection Error when using Mongoose 如何编写用于使用mongoose,mocha和chai将对象插入和检索到mongodb的单元测试? - How to write a Unit test for inserting and retrieving an object to mongodb using mongoose, mocha and chai? 使用 Mongoose 连接到 MongoDB - Connecting to MongoDB using Mongoose 在节点/猫鼬中使用$ inc时的MongoDB数字精度 - MongoDB number precision when using $inc in node/mongoose 在 mongodb 中的两个 collections 中保存消息时使用“猫鼬事务” - Using `mongoose transactions` when saving message in two collections in mongodb 对于某些没有任何包含点的键的对象,使用node.js插入MongoDB的键错误会失败吗? - Key error inserting to MongoDB using node.js fail for some objects with out any key containing a dot? 如何在 MongoDB 和 Mongoose 中创建外键? - How to create a foreign key in MongoDB with Mongoose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM