In our project we have existing database scheme and there is no longer possible to change it. Is where workaround to use reserved key in schema? We need to use options
key.
Same for "type" key
Thanks !
See https://github.com/LearnBoost/mongoose/issues/528 ??? (one year ago !)
Why is it not possible to change your schema or type key name?
Because it is an existing distributed project, written not only on NodeJS, scheme has already been defined and everything is already running.
Because i want to store native geoJSON data, and geoJSON fields match reserved keywords
This issue should be taken into account and resolved for v4
Why can't mongoose just use a prefix like _ (underscore) or $ (dollar) for internal functionality. I think thats a common technique in those cases. "options" is a totally common property name.
Ability to use options
as a key was added for 4.0.0-rc0, see #1416
I agree with @smonkey72. I think mongoose should use prefixed names of reserved keys like this:
var blogSchema = new Schema({
title: String,
author: String,
body: String,
date: { // here
$type: Date,
$default: Date.now
},
meta: {
votes: Number,
favs: Number
}
});
Or use properties
for object fields (like JSON Schema):
var blogSchema = new Schema({
title: String,
author: String,
body: String,
date: {
type: Date,
default: Date.now
},
meta: { // here
properties: {
votes: Number,
favs: Number
}
}
});
I agree, that's definitely something we'd at least like to make configurable in the near future. See #3245
@vkarpov15 Do you plan to use only prefixed with $
names of reserved keywords?
typeKey
is a bad solution because it solves problem only with type
key. But we have other keywords like default
, index
, ...
Field names must not start with $
in MongoDB, so we can safely use it for internal keywords:
var schema = new Schema({
date: {
$type: Date,
$default: Date.now
}
});
The $
limitation was only introduced in mongodb 2.6, so this isn't 100% safe yet. However, we're going to do more and more of prefixing reserved keywords with $
going forward. Hard to do without backwards breaking changes though.
Why is it closed? Has this issue being fixed?
If yes, could you provide the answer on how to use reserved keywords as properties now (because the mangoose official doc do not provide any answer regarding this issue)?
This issue was specifically about using options as a schema key, which is ok right now. As for using reserved keywords, we don't plan on allowing using things like save
as schema keys right now, because that would require massive api changes
'type' keyword is not working when I tried to set a Geojson format object with required key
This doesn't work:
var LocationSchema = new mongoose.Schema({
name: {
type: String,
unique: true,
default: '',
required: true
},
location: {
type: {
type: {
type: String,
default: 'Point'
},
coordinates: {
type: [Number],
default: [0, 0]
}
},
required: true
}
})
When removed the required true, it does work:
var LocationSchema = new mongoose.Schema({
name: {
type: String,
unique: true,
default: '',
required: true
},
location: {
type: {
type: String,
default: 'Point'
},
coordinates: {
type: [Number],
default: [0, 0]
}
}
})
@WangHansen can you clarify what you mean by "doesn't work"?
hmm I just faced this issue, in my case I'm fetching data from an external API and storing it into mongoDB. The external API however returns fields that has reserved keywords like "alias" and "type", which made mongoose throw a cast validation error when trying to store the data in mongo, it was difficult to find what the actual issue is.
Are there any workarounds for this problem?
@khaledosman You can define customized type key word: https://mongoosejs.com/docs/guide.html#typeKey
Most helpful comment
I agree with @smonkey72. I think mongoose should use prefixed names of reserved keys like this:
Or use
properties
for object fields (like JSON Schema):