I was playing with my nodjs and mongoDB. I found an error that I can not update a object.
const { MongoClient, ObjectID } = require('mongodb');
// or as an es module:
// import { MongoClient } from 'mongodb'
const url =
'mongodb srv://<user>:<password>@cluster0.uw6pe.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
console.log(url);
const client = new MongoClient(url);
// Database Name
const dbName = 'studio';
export async function insertData(_id: string) {
// Use connect method to connect to the server
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection('shops');
return new Promise(function (resolve, reject) {
collection.updateOne(
{ _id: ObjectID(_id) },
{
$set: {
address: {
type: 'Point',
coordinates: [123, 321],
},
},
},
(err, res) => {
if (err) reject(err);
resolve(res);
},
);
});
}
I was trying to insert an address(Object), by using NestJS. And got this error.
So I wonder what I did wrong. I could update key : value, but I can't update key:object, If I enter an empty object, it works.
Is there anyone who has an answare?
CodePudding user response:
The issue is that coordinates: [123, 321]
aren't valid coordinates, this is because 321
isn't a valid latitude value. Latitudes are between -90 and 90, whereas longitudes can be between -180 and 180.
The clue is in the error message: "longitude/latitude out of bounds"
.
If you make sure that your coordinates valid you shouldn't run into this error.
Useful docs for reading: