I want to store latitude and longitude coordinates for a user. They may have more than one so I am making it an array of objects. I just wanted to ask if this structure is okay for storing an array of objects for a user or should it be changed to something different/better?
In my User mongoose schema:
places: [
{
location: {
latitude: {
type: Number,
},
longitude: {
type: Number,
},
},
},
],
My controller:
exports.addLocation = async (req, res) => {
const { latitude, longitude, userId } = req.body;
const user = await User.findById(userId);
if (!user) {
return res.status(404).send("No user found");
}
user.places.push({ location: { longitude: longitude, latitude: latitude } });
await user.save();
res.status(201).send(user);
};
When I look in Mongo DB Atlas I can see that each object in the array has its own _id.
{
"_id": {
"$oid": "5f2d9ec5053d4a754d6790e8"
},
"name": "Bob",
"email": "[email protected]",
"__v": 31,
"places": [{
"_id": {
"$oid": "5f3577d511d3f56b7082d320"
},
"location": {
"longitude": 48.137565,
"latitude": 11.575502
},
"saved_at": {
"$date": "2020-08-13T17:26:45.312Z"
}
}, {
"_id": {
"$oid": "5f3577ee11d3f56b7082d321"
},
"location": {
"longitude": 48.863129,
"latitude": 2.294873
},
"saved_at": {
"$date": "2020-08-13T17:27:10.594Z"
}
}]
}

