向模式添加新字段后如何更新旧文档?
How to update old documents after adding new fields to the schema?
假设我有一个 user
架构,如下所示:
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true
}
});
然后一些人在网站上注册并创建了 user
个文档。
然后在将来的某个时候,我将这些附加字段添加到 user
架构中:
joinedAt: {
type: Data,
default: Date.now,
},
about: {
type: String,
required: true,
}
我应该如何更新旧数据?我来自 django
而在 django
我只是 运行 python3 manage.py makemigrations
如果不需要或有默认值(例如joinedAt
在这种情况下)。
但如果它没有(如本例中的 about
字段),那么它会询问我现有字段的值应该是多少。
Node.js是怎么做的?
只需编写一个 MongoDb 更新查询,如下所示:
let about = "Whatever you want";
db.users.updateMany(
{
joinedAt: { $exists: false },
about: { $exists: false },
},
{
$set: {
joinedAt: new Date(),
about: about
}
}
);
或
如果您想要 Node.js 脚本,那么:
步骤 1:创建文件 user_migration.js
:
const MongoClient = require('mongodb').MongoClient;
const DB_URI = "mongodb://localhost:27017/myDB";
const options = {
useNewUrlParser: true
};
MongoClient.connect(DB_URI, options, (err, client) => {
if (err) {
console.log("ERROR: Failed to connect to database.");
console.log(err);
return;
}
let dbName = DB_URI.split("/", -1).pop();
let db = client.db(dbName);
console.log(`Connected to ${dbName} database successfully.`);
let about = "Whatever you want";
db
.collection('users')
.updateMany(
{
joinedAt: { $exists: false },
about: { $exists: false },
},
{
$set: {
joinedAt: new Date(),
about: about
}
}
)
.then(res => {
// console.log(res);
console.log(`${res.result.nModified} documents updated successfully.`);
console.log("Database connection closed.");
client.close();
})
.catch(err => {
console.log(JSON.stringify(err));
console.log("Database connection closed.");
client.close();
});
});
第 2 步:运行 来自终端的文件:
node C:path\to\your\source_code\user_migration.js
假设我有一个 user
架构,如下所示:
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true
}
});
然后一些人在网站上注册并创建了 user
个文档。
然后在将来的某个时候,我将这些附加字段添加到 user
架构中:
joinedAt: {
type: Data,
default: Date.now,
},
about: {
type: String,
required: true,
}
我应该如何更新旧数据?我来自 django
而在 django
我只是 运行 python3 manage.py makemigrations
如果不需要或有默认值(例如joinedAt
在这种情况下)。
但如果它没有(如本例中的 about
字段),那么它会询问我现有字段的值应该是多少。
Node.js是怎么做的?
只需编写一个 MongoDb 更新查询,如下所示:
let about = "Whatever you want";
db.users.updateMany(
{
joinedAt: { $exists: false },
about: { $exists: false },
},
{
$set: {
joinedAt: new Date(),
about: about
}
}
);
或
如果您想要 Node.js 脚本,那么:
步骤 1:创建文件 user_migration.js
:
const MongoClient = require('mongodb').MongoClient;
const DB_URI = "mongodb://localhost:27017/myDB";
const options = {
useNewUrlParser: true
};
MongoClient.connect(DB_URI, options, (err, client) => {
if (err) {
console.log("ERROR: Failed to connect to database.");
console.log(err);
return;
}
let dbName = DB_URI.split("/", -1).pop();
let db = client.db(dbName);
console.log(`Connected to ${dbName} database successfully.`);
let about = "Whatever you want";
db
.collection('users')
.updateMany(
{
joinedAt: { $exists: false },
about: { $exists: false },
},
{
$set: {
joinedAt: new Date(),
about: about
}
}
)
.then(res => {
// console.log(res);
console.log(`${res.result.nModified} documents updated successfully.`);
console.log("Database connection closed.");
client.close();
})
.catch(err => {
console.log(JSON.stringify(err));
console.log("Database connection closed.");
client.close();
});
});
第 2 步:运行 来自终端的文件:
node C:path\to\your\source_code\user_migration.js