使用nodejs请求Mongodb并存储对象时,MONGODB会将对象永久保存吗?
时间: 2023-02-07 21:19:20 浏览: 93
是的,MongoDB会将对象永久保存。MongoDB是一种文档数据库,它将文档(即对象)存储在集合(类似于关系数据库中的表)中,并使用专用的数据存储引擎将文档永久地写入磁盘。这意味着,只要你没有明确地删除文档,MongoDB就会将其保存在数据库中。
在Node.js中使用MongoDB驱动程序进行对象存储时,你可以使用`insertOne`或`insertMany`方法将对象插入集合中。例如:
```
const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
const collection = client.db("test").collection("devices");
// perform actions on the collection object
collection.insertOne({ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }, function(err, res) {
console.log("Document inserted");
client.close();
});
});
```
在这段代码中,我们使用`insertOne`方法将一个对象插入`devices`集合中。当文档被成功插入后,MongoDB会将其永久保存在数据库中,除非你手动删除它。