Mongoose Schema for geoJson coordinates - javascript

I tried to create a schema for geojson but have had some problems with syntax for coordinates.
Here's my current code:
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: []
}
});
I tried using [] (empty array), it creates '' and [Number,Number] but it doesn't work.
My question is: how do I have to construct my schema so as result I will get
coordinates: [ 3.43434343, 5.543434343 ]
without quotation marks, is this possible?
Express Route
app.post('/mountain_rescue', function (req, res){
new rescueData({properties:{title: req.body.title, description: req.body.description},geometry:{
coordinates:req.body.coordinates}}).save(function (e, result) {
console.log(result);
});
res.redirect('/mountain_rescue');
});
View
<div id="AddingPanel">
<form method="post" action="mountain_rescue" >
Title:<input type="text" name="title">
Description:<textarea type="text" name="description"></textarea>
Coordinates:<input type="text" name="coordinates">
<button type="submit">Add</button>
</form>

A GeoJSON field has to be included a geometry type as a string. So a GeoJSON field must be defined like the following;
geometry: { type: { type: String }, coordinates: [Number] }
or if you want to define a default value you might use the below line;
geometry: { type: { type: String, default:'Point' }, coordinates: [Number] }
Good luck..

Like this;
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: { type: [Number], index: '2dsphere'}
}
});
Here is your update route handler, it converts coordinates string to number array;
app.post('/mountain_rescue', function (req, res) {
new rescueData({
properties: {
title: req.body.title, description: req.body.description
},
geometry: {
coordinates:req.body.coordinates.split(',').map(Number)
}
}).save(function (e, result) {
console.log(result);
});
res.redirect('/mountain_rescue');
});

try this:
var DataSchema = new Schema({
properties: {
title: { type: String, required: true },
description: { type: String, required: true },
date: { type:Date, default:Date.now }
},
geometry: {
coordinates: {type: Array, required: true}
}
});

Related

What is the best way to attach properties to mongoose documents

I have an array of posts and each post contains an author id. I want to loop through each post and find author from User model by using author id and then attach it to post. What is the best and efficient way to do it. I am currently doing it this way, but it decreases the performance. Thanks.
posts = await Promise.all(
posts.map(async post => {
post.author = await User.findById(post.author).lean();
return post;
})
);
// POST SCHEMA
const postSchema = new mongoose.Schema({
author: {
type: String,
required: true
},
body: {
type: String,
required: true
},
post_image: {
url: String,
public_id: String,
width: Number,
height: Number
},
date_created: {
type: Date,
default: Date.now()
}
});
// USER SCHEMA
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
register_date: {
type: Date,
required: true,
default: Date.now()
},
friends: {
type: Array,
default: []
}
});
// NEW POST SCHEMA
const postSchema = new mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
required: true
},
body: {
type: String,
required: true
},
post_image: {
url: String,
public_id: String,
width: Number,
height: Number
},
date_created: {
type: Date,
default: Date.now()
}
});
// USER SCHEMA
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
register_date: {
type: Date,
required: true,
default: Date.now()
},
friends: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'users',
required: true
}],
});
You can use auto population of mongo object in mongoose. It won't cause performance issues as it uses id index. Similar to this doc: https://mongoosejs.com/docs/populate.html
Your query will look like this:
const post = await Post.find({ author: { $in: req.user.friends }})
.populate('author')
.exec();
console.log(post);
Or you can use aggregate according to this document: https://mongoosejs.com/docs/api/aggregate.html
Your query will then look like:
const query = [
{ $match: { author: { $in: req.user.friends } } },
{ $lookup: { from: "users", localField: "author", foreignField: "_id", as: "authorDetails" } },
{ $unwind: "authorDetails" },
]
const post = await Post.aggregate(query).exec();
console.log(post);

Aggregation query with $match works in MongoDB Compass but not on my Node.js app

I have a Booking model and an Event model. I'm attempting to query and check if there are any bookings that have a specific Event._id and User._id already, as to stop the creation of a duplicate Booking for that user and event. The aggregation query works on MongoDB compass, however it's only giving me an empty array when I try the query in the Node.js app
Models
Booking
const BookingSchema = new Schema({
amount: {
type: Number,
required: 'Please supply a number of people',
},
event: {
type: mongoose.Schema.ObjectId,
ref: 'Event',
required: 'Must give an event!',
},
booker: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: 'You must supply the booker!',
},
confirmed: {
type: Boolean,
default: false,
},
});
Event
const eventSchema = new Schema(
{
title: {
type: String,
trim: true,
required: 'You must add an event name!',
},
description: {
type: String,
trim: true,
},
slug: String,
created: {
type: Date,
default: Date.now,
},
date: {
type: Date,
min: Date.now,
required: 'Please enter a valid event Date!',
},
minCapacity: {
type: Number,
required: 'Please enter a correct min capacity for your event!',
},
maxCapacity: {
type: Number,
required: 'Please enter a correct max capacity for your event!',
},
price: Number,
location: {
type: {
type: String,
default: 'Point',
},
coordinates: [
{
type: Number,
required: 'You must supply coords!',
},
],
address: {
type: String,
required: 'Please enter a valid address!',
},
},
photo: String,
author: {
type: Schema.ObjectId,
ref: 'User',
required: 'You must supply an author!',
},
available: Boolean,
// attendees: [User], you can do through virtuals
},
{
toJSON: { virtuals: true },
toObject: { virtuals: true },
}
);
eventSchema.virtual('bookings', {
ref: 'Booking', // what model is linked?
localField: '_id', //what field on model
foreignField: 'event', //which field on Booking?
});
module.exports = mongoose.model('Event', eventSchema);
Query
exports.createBooking = async (req, res) => {
req.body.booker = req.user._id;
req.body.event = req.params.id;
const bookings = await Booking.aggregate(
[
{
$match: {
event: req.params.id,
},
},
{ $count: 'bookings' },
],
);
return res.json(bookings);
};
Thank you in Advanced! And if there's any other information you'd like, please just let me know.
You have to cast your id from String to ObjectId
const ObjectID = require('mongodb').ObjectID
[
{ "$match": {
"event": ObjectId(req.params.id),
}},
{ "$count": "bookings" },
]

Updating nested array's field

I have a schema
const RoomSchema = mongoose.Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true
},
author: {
type: String,
required: true
},
resource: {
type: String
},
posts: {
type: Array,
default: []
},
url: {
type: String
},
created_at: {
type: String,
required: true
}});
Field 'posts' is another document in my db, defined by the following schema:
const PostSchema = mongoose.Schema({
header: {
type: String,
required: true
},
body: {
type: String,
required: true
},
author: {
username: {type:String, required: true},
_id: {type:String, required:true}
},
room: {
type: String,
required: true
}});
So, I'm trying to create a query that would update fields of certain post inside posts array inside room. I've already tried suggested here, thought without results. I would appreciate any help on the subject
Room.update({ '_id': roomId, 'posts._id': postId },
{ $set: { 'posts.$.header': newHeader, 'posts.$.body': newBody } },
callback);

How to get relationship model in Keystone.js

So I try to get 'Pronouns' model from 'Sentence' model from 'TestGenerator' model. And I have id's, but no model.
TestGenerator Model
var TestGenerator = new keystone.List('TestGenerator', {
map: { name: 'title' },
autokey: { path: 'slug', from: 'title', unique: true }
});
TestGenerator.add({
title: { type: String, required: true },
sentences: { type: Types.Relationship, ref: 'Sentence', many: true },
});
TestGenerator.register();
var Sentence = new keystone.List('Sentence', {
map: { name: 'title' },
autokey: { path: 'slug', from: 'title', unique: true }
});
Sentence Model
Sentence.add({
title: { type: String, required: true },
pronouns: { type: Types.Relationship, ref: 'Pronoun', many: true },
verbs: { type: Types.Relationship, ref: 'Verb', many: true },
});
Sentence.relationship({ ref: 'TestGenerator', path: 'sentences' });
Sentence.register();
Ponoun model
var Pronoun = new keystone.List('Pronoun', {
map: { name: 'english' },
autokey: { path: 'slug', from: 'english', unique: true },
});
Pronoun.add({
english: { type: String },
russian: { type: String }
});
Pronoun.relationship({ ref: 'Sentence', path: 'pronouns' });
Pronoun.register();
And my controller
view.on('init', function (next) {
var q = keystone.list('TestGenerator').model.findOne({
slug: locals.filters.test_genetation,
}).populate('sentences');
q.exec(function (err, result) {
locals.testGenerator = result;
locals.current_sentence = result.sentences[0];
locals.data.englishSentence = locals.current_sentence.pronouns;
console.log(locals.data.englishSentence);
next(err);
});
});
And "locals.data.englishSentence" return
["5739bd696ef7d78d16e9e0e5","573ac7645e3d7d7210f1c4be"]
So how I can fix it? Don't understand.
This is related to mongoose, not Keystone actually. Mongoose doesn't provide deep population, you'll need a separate plugin like e.g. mongoose-deep-populate
Have you tried explicitly including pronouns in the populate() call?
var q = keystone.list('TestGenerator').model.findOne({
slug: locals.filters.test_genetation,
})
.populate('sentences sentences.pronouns')
.exec(function (err, result) {
...
});

Manipulating Mongoose/MongoDB Array using Node.js

I've noticed there's little documentation and info about how I should manipulate an array of objects using Mongoosejs.
I have the following model/Schema for an User:
'use strict';
/**
* Module Dependencies
*/
var bcrypt = require('bcrypt-nodejs');
var crypto = require('crypto');
var mongoose = require('mongoose');
/**
* Custom types
*/
var ObjectId = mongoose.Schema.Types.ObjectId;
var userSchema = new mongoose.Schema({
email: { type: String, unique: true, index: true },
password: { type: String },
type: { type: String, default: 'user' },
facebook: { type: String, unique: true, sparse: true },
twitter: { type: String, unique: true, sparse: true },
google: { type: String, unique: true, sparse: true },
github: { type: String, unique: true, sparse: true },
tokens: Array,
profile: {
name: { type: String, default: '' },
gender: { type: String, default: '' },
location: { type: String, default: '' },
website: { type: String, default: '' },
picture: { type: String, default: '' },
phone: {
work: { type: String, default: '' },
home: { type: String, default: '' },
mobile: { type: String, default: '' }
}
},
activity: {
date_established: { type: Date, default: Date.now },
last_logon: { type: Date, default: Date.now },
last_updated: { type: Date }
},
resetPasswordToken: { type: String },
resetPasswordExpires: { type: Date },
verified: { type: Boolean, default: true },
verifyToken: { type: String },
enhancedSecurity: {
enabled: { type: Boolean, default: false },
type: { type: String }, // sms or totp
token: { type: String },
period: { type: Number },
sms: { type: String },
smsExpires: { type: Date }
},
friends: [{
friend: { type: ObjectId, ref: 'User' },
verified: { type: Boolean, default: false }
}]
});
/* (...) some functions that aren't necessary to be shown here */
module.exports = mongoose.model('User', userSchema);
So as you can check I defined Friends inside User like this:
friends: [{
friend: { type: ObjectId, ref: 'User' },
verified: { type: Boolean, default: false }
}]
Now the question is how can I add, edit and delete this array in a Node.js script?
BOTTOMLINE: How can I manipulate arrays that are inside MongoDB Schemas, using Node.js and Mongoose.js? Do I always have to create a Schema function or can I access it directly?
EDIT (13/07/2014): So far I've created a HTTP GET that gives me the array like this:
app.get('/workspace/friends/:userid', passportConf.isAuthenticated, function (req, res) {
User.find({_id: req.params.userid}, function (err, items) {
if (err) {
return (err, null);
}
console.log(items[0].friends);
res.json(items[0].friends);
});
});
But this only returns an array of friendIds, but what if I want to create some sort of '/workspace/friends/:userid/del/:friendid' POST, or add POST. I can't seem to figure out how I can get this done.
You can do something like following
app.get('/workspace/friends/:userid/delete/:friendId', passportConf.isAuthenticated, function (req, res) {
User.findOne({_id: req.params.userid}, function (err, user) {
if (err) {
return (err, null);
}
for (var i = 0; i < user.friends.length; i++) {
if (user.friends[i]._id === req.params.friendId) {
user.friends = user.friends.splice(i,1)
}
}
user.save(function(err, user, numAffected){
if (!err )res.json(user)
res.send('error, couldn\'t save: %s', err)
})
});
});
What it says in mongoose docs is that
"The callback will receive three parameters, err if an error occurred, [model] which is the saved [model], and numberAffected which will be 1 when the document was found and updated in the database, otherwise 0.
The fn callback is optional. If no fn is passed and validation fails, the validation error will be emitted on the connection used to create this model."
If you need to manipulate arrays, you should convert these in objects before.
User.findOne({_id: req.params.userid}, function (err, user) {
if (err) {
return (err, null);
}
var user = user.toObject();
//... your code, an example =>
delete user.friends;
res.json(user);
});
Regards, Nicholls

Categories

Resources