var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var url = "https://i.stack.imgur.com/34AD2.jpg";
var UserSChema = new mongoose.Schema({
firstName: String,
lastName: String,
email: String,
profilePic: {type: String, default: url},
username: String,
password: String,
isAdmin: {type: Boolean, default: false}
});
UserSChema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSChema);
上面提到的是我的代码,我尝试将字符串设置为url并使用它,但是它不起作用。每当我去检查mongo shell中的值时,profilePic将为空。
如果您的mongo集合中已经有文档,它将不会更新它们在数据库中的图像URL;当您保存任何新数据时,它将添加默认URL;如果使用mongoose查找,它将添加旧记录。他们。
因此,在使用猫鼬时,您始终会在代码中获得默认值https://i.stack.imgur.com/34AD2.jpg
,但是如果在mongo shell中看到,旧文档的profilePic将为空。
如果还要在所有旧文档中更新profilePic的默认值,请在Mongo控制台中运行以下查询。
db.getCollection('users').updateMany({},{$set:{"profilePic": "https://i.stack.imgur.com/34AD2.jpg"}})