Mongoose giving error for NOT required Geo spatial field - node.js

I am using Mongoose Schema and simplified version looks like:
const newSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
location:{
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number],
}
});
const newModel = mongoose.model('NewModel', newSchema);
When I try to save a new document using this schema:
newModel.create({
"name": "Default name"
});
It is giving
error:
"Can't extract geo keys: { _id: ObjectId('5e8ed5ddf4781c24d0836b6e'), location: { type: \"Point\" },
name:\"Default name\"} Point must be an array or object"
However, when I fill the location field, it works well. I am wondering why Schema checking for the NOT required field.

var GeoJSON = require('mongoose-geojson-schema');
var mongoose = require('mongoose');
const newSchema = new mongoose.Schema({
name:{
type: String,
required: true
},
location:mongoose.Schema.Types.GeoJSON
});
const newModel = mongoose.model('NewModel', newSchema);
and create data like:
newModel.create({
name: "Default name",
location: {
type: "Point",
coordinates: [longitude, latitude]
}
});
actually the problem occur due to you specified the default type is Point (that pushed by mongoose always if null or not specified) but you did't pass coordinates corresponding, and the point should be with coordinate

Related

Mongoose Populate Array of ObjectIDs that utilizes refPath returns empty array

issue.js
const mongoose;
const schema = new mongoose.Schema({
docs: {
type: [mongoose.Schema.Types.ObjectId],
refPath: 'coll',
required: true
},
coll: {
type: String,
required: true,
enum: ['Product', 'Service']
}
});
module.exports = mongoose.model('Issue', schema);
partial index.js
try{
let issues = await Issue.find().populate('docs');
console.log(issues);
//every issue in issues, has an empty 'docs' array, when it should consist of products/services that exist
}catch(e){}
Not sure why arrays is making it not work, I tried making the docs field as singular ObjectId, and it worked fine. Only thing I could think of is that because it's an array, the refPath has to be different, but I'm not sure what I could add, as I tried using 'this.coll'.
I see an error in your schema. Not sure if that is the issue, but you can try.
const mongoose;
const schema = new mongoose.Schema({
docs: [{
type: mongoose.Schema.Types.ObjectId, // <-- This line
refPath: 'coll',
required: true
}],
coll: {
type: String,
required: true,
enum: ['Product', 'Service']
}
});

How do I index a field on a mongoose Schema that uses a discriminator?

The error started after I started using the discriminator.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Base = require("../config/Base");
const Refill = Base.discriminator(
"Refill",
new Schema({
cylinderSize: { type: Number, required: true },
cylinderSwap: { type: Boolean, required: true },
quantity: { type: Number, required: true },
location: {
type: { type: String },
coordinates: [Number]
}
})
);
Refill.index({ location: "2dsphere" });
module.exports = mongoose.model("Refill");
This returns the error Refill.index is not a function
In Mongoose, indexes must be created on schemas, not models. In your case, the Refill object is a model. One approach is to implement this in three steps:
Create the schema
Add the index to the schema
Create the model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Base = require("../config/Base");
const refillSchema =
new Schema({
cylinderSize: { type: Number, required: true },
cylinderSwap: { type: Boolean, required: true },
quantity: { type: Number, required: true },
location: {
type: { type: String },
coordinates: [Number]
}
});
refillSchema.index({ location: "2dsphere" });
const Refill = Base.discriminator("Refill", refillSchema);
module.exports = mongoose.model("Refill");
I just took out the Refill.index({ location: "2dsphere" }); and the rest of my code is working fine apparently indexing that field wasn't necessary.

Mongoose - Save array of strings

I can't save an array of strings into my DB using Mongoose.
(Note all code below is simplified for ease of writing here)
So i declare a variable of a person schema I have:
var newPerson = new Person ({
tags: req.body.tags
});
The schema itself looks like:
var personSchema = new mongoose.Schema({
tags: Array
});
And when it comes to saving its just a simple:
newPerson.save(function(err) {
//basic return of json
});
So using Postman I send in an array in the body - however everytime I check the DB, it just shows one entry with the array as a whole i.e. how I sent it:
Any ideas what extra I'm supposed to do?
Write up from my comment:
The way to specify an array of strings in mongoose is like so:
var personSchema = new mongoose.Schema({
tags: [{
type: String
}]
However, the problem here is most-likely to do with Postman as it is sending the 'array' as a string. You can check this by checking the type of req.body.tags like so:
console.log(typeof req.body.tags)
If this returns a String, make sure to set the content-type in Postman to JSON as seen in this screenshot rather than the default 'form-data' option.
var schema = new Schema({
name: String,
binary: Buffer,
living: Boolean,
updated: { type: Date, default: Date.now },
age: { type: Number, min: 18, max: 65 },
mixed: Schema.Types.Mixed,
_someId: Schema.Types.ObjectId,
decimal: Schema.Types.Decimal128,
array: [],
ofString: [String],
ofNumber: [Number],
ofDates: [Date],
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [Schema.Types.Mixed],
ofObjectId: [Schema.Types.ObjectId],
ofArrays: [[]],
ofArrayOfNumbers: [[Number]],
nested: {
stuff: { type: String, lowercase: true, trim: true }
},
map: Map,
mapOfString: {
type: Map,
of: String
}
})
// example use
var Thing = mongoose.model('Thing', schema);
var m = new Thing;
m.name = 'Statue of Liberty';
m.age = 125;
m.updated = new Date;
m.binary = Buffer.alloc(0);
m.living = false;
m.mixed = { any: { thing: 'i want' } };
m.markModified('mixed');
m._someId = new mongoose.Types.ObjectId;
m.array.push(1);
m.ofString.push("strings!");
m.ofNumber.unshift(1,2,3,4);
m.ofDates.addToSet(new Date);
m.ofBuffer.pop();
m.ofMixed = [1, [], 'three', { four: 5 }];
m.nested.stuff = 'good';
m.map = new Map([['key', 'value']]);
m.save(callback);
On Schema:
( Since you have mentioned in the problem that it is an array of strings )
var personSchema = new mongoose.Schema({
tags:{
type:[String],
required: true
}
});
On Postman:
{
"tags": ["css", "javascript", "mongoose", "node"]
}
On MongoDB
{
"tags":["css", "javascript", "mongoose", "node"]
}
Similarly, you can create other types of primitive arrays and document arrays in the mongoose schema as:
({
toys: [ToySchema],
buffers: [Buffer],
strings: [String],
numbers: [Number]
// ... etc
});
Try changing the schema to
var personSchema = new mongoose.Schema({
tags: [{type: String}]
});
or you can use Mixed type
var personSchema = new mongoose.Schema({
tags: mongoose.Schema.Types.Mixed
});
EDIT
i think the problem is with assignment. Use:
person.tags.push("string to push");
On Schema
techs: Array
On Postman
"techs": ["express","rect","html","css","scss"]
On DB (MongoDB)
"techs" : [
"epxress",
"rect",
"html",
"css",
"scss"
]
var personSchema = new mongoose.Schema({
tags: [{type: String}]
});
Use this in the schema.
Saving the Array:
var etc = new modename({yourprimaryid: primaryid});
for (var i = 0; i < tag.length; i++) {
etc.tag.push(tag[i]);
}
etc.save(function(err) {
//whatever you want here
}
Define a Schema:
const schema = new Schema({
name: { type: String, required: true },
tags: [String]
});
In postman add each element separately using the array syntax below
name:Thing
tags[]:task
tags[]:other
tags[]:thing
Return Data:
{
"__v": 0,
"name": "Thing",
"_id": "5a96e8d0c7b7d1323c677b33",
"tags": [
"task",
"other",
"thing"
]
}
this will also work
var personSchema = new mongoose.Schema({
tags: {
type: [String], default: []
}
});
Firstly, as many people have noted, the schema needs to change to indicate that the tags field is intended to hold an array of strings, and not just a single one. So that needs to change to:
var personSchema = new mongoose.Schema({
tags: [String]
});
The other thing you need to keep in mind (and which caused me a lot of trouble), is that when saving, make sure to use a fresh array for the tags field. For example, this won't work:
person.tags[0] = "new tag";
person.save();
Instead, you need to do something like:
person.tags = person.tags.slice(); // Clone the tags array
person.tags[0] = "new tag";
person.save();
Hope this helps.
I had a simialr problem,
In the model, do this :
tags : {[String], default: undefined}
So that it defaults to undefined unstead of an empty array,
and instead of this:
const person = new Person({
tags : req.body.tags
});
Do this :
const person = new Person();
person.tags = req.body.tags;
My requirement;
ingredients: Type Array of Strings
Solution:
ingredients: {
type: [String],
},
const productSchema = new mongoose.Schema(
{
name: {
type: String,
},
description: {
type: String,
},
price: {
type: String,
},
categoryId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category",
},
sellerId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Seller",
},
discount: {
type: String,
},
status: {
type: String,
default: "active",
enum: ["active", "inactive", "deleted"],
},
images: {
type: Array,
required: true,
},
},
{ timestamps: true }
);

Location in mongoose, mongoDB

Whenever I try to store a location in my mongodb it doesn't show, so I guess I'm doing something wrong.
I can't find any documentation on how to store a location in mongoose so I'm just gonna ask it here.
I first create my model:
var eventSchema = mongoose.Schema({
name : String,
creator : String,
location : { type: [Number], index: '2dsphere'},
});
Then I try to add it to my database:
var newevent = new event({
name: name,
creator: tokenUser,
location : { type: "Point", coordinates: [longitude, latitude] },
});
When I look in my database everything is stored except the location ...
I fixed it myself.
I did this in my model:
loc : { type: {type:String}, coordinates: [Number]},
Underneath I made it a 2dsphere index.
eventSchema.index({loc: '2dsphere'});
And to add data to it:
loc: { type: "Point", coordinates: [ longitude, latitude ] },
Looks like your comment is correct (maybe), but the syntax for the index schemetype
here: http://mongoosejs.com/docs/api.html#schematype_SchemaType-index
It only accepts Object, Boolean, String
The correct syntax should be I think
var eventSchema = new Schema({
location: { type: [Number], index: { type: '2dsphere', sparse: true}}
)
based on the example in the docs.
This way i find it simpler.
const GeoSchema = mongoose.Schema({
type: {
type: String,
default: "Point",
},
coordinates: {
type: [Number], //the type is an array of numbers
index: "2dsphere"
}
})
const EventSchema = mongoose.Schema({
name: String,
creator: String,
location: GeoSchema
})
And when entering data
Event({
name,
creator,
location: { type: "point", coordinates: [longitude, latitude] }
})
Citing from the mongoose documentation for defining coordinates in schema, I'd like to make a tiny addition to #Laurenswuyts' answer for the 'type' property.
const exampleSchema = new mongoose.Schema({
location: {
type: {
type: String, // Don't do `{ location: { type: String } }`
enum: ['Point'], // 'location.type' must be 'Point'
required: true
},
coordinates: {
type: [Number],
required: true
}
}
});
Instead of leaving the 'type' property of 'location' open-ended with 'type: String', defining it with a single option enum 'Point' and setting 'required' to 'true' makes the code more robust. Then you would create index and add data the same way #Laurenswuyts did.

Does applying a 2dsphere index on a mongoose schema force the location field to be required?

I have a mongoose schema and model defined as follows:
var mongoose = require('mongoose')
, Schema = new mongoose.Schema({
email: {
index: {
sparse: true,
unique: true
},
lowercase: true,
required: true,
trim: true,
type: String
},
location: {
index: '2dsphere',
type: [Number]
}
})
, User = module.exports = mongoose.model('User', Schema);
If I attempt:
var user = new User({ email: 'user#example.com' });
user.save(function(err) {
if (err) return done(err);
should.not.exist(err);
done();
});
I receive the error message:
MongoError: Can't extract geo keys from object, malformed geometry?:{}
Despite the location field in this schema not being required, it seems to be acting as such anyways. I have tried adding default: [0,0] which does circumvent this error, however it seems like a bit of a hack, as this is clearly not a good default, and ideally the schema would not require the user to have a location at all times.
Do geospatial indexes with MongoDB / mongoose imply that the field being indexed is required?
For mongoose 3.8.12, you set the default value:
var UserSchema = new Schema({
location: {
type: {
type: String,
enum: ['Point'],
default: 'Point',
},
coordinates: {
type: [Number],
default: [0, 0],
}
}
});
UserSchema.index({location: '2dsphere'});
By default, a property declared an array receives a default empty array to work with. MongoDB has started validating geojson fields and yells about empty arrays. The work around is to add a pre save hook to the schema that checks for this scenario and fixes up the document first.
schema.pre('save', function (next) {
if (this.isNew && Array.isArray(this.location) && 0 === this.location.length) {
this.location = undefined;
}
next();
})
The correct way to create a schema with geolocation and run geospartial queries was
var UserSchema = new Schema({
location: {
type: {
type: String,
enum: ["Point"], // 'location.type' must be 'Point'
},
coordinates: {
type: [Number]
}
}
});
UserSchema.index({location: '2dsphere'});
Be careful this is important to do, the index 2dsphere in the
location.coordinates, if you want to run geospartial queries!
The only way I have resolved this issue was changing the index type from
GEO:{
type: [Number],
index: '2dsphere'
}
to
GEO:{
type: [Number],
index: '2d'
}
It was a nightmare

Resources