【发布时间】:2021-12-18 06:26:36
【问题描述】:
我在玩我的 nodjs 和 mongoDB。我发现无法更新对象的错误。
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);
},
);
});
}
我试图通过使用 NestJS 插入地址(对象)。并得到了这个错误。
所以我想知道我做错了什么。我可以更新键:值,但我不能更新键:对象, 如果我输入一个空对象,它会起作用。
有人有答案吗?
【问题讨论】:
标签: javascript node.js mongodb nestjs