简体   繁体   English

如何在模式中未存在的MongoDB中的现有文档中定义新字段? (Node.js)

[英]How to define a new field in a pre-existing document in MongoDB that isn't in the schema? (Node.js)

I am making a discord bot using MongoDB and I want to add some user-specific fields to an already existing document in MongoDB that aren't in the schema. 我正在使用MongoDB做一个不和谐的机器人,我想将一些特定于用户的字段添加到不在模式中的MongoDB中已经存在的文档中。 There is a lot of data I am planning to add and I wanted it to be added automatically depending on what the user does for efficiency. 我打算添加很多数据,我希望根据用户为提高效率而自动添加数据。

I tried to use findOneAndUpdate to set the new field and its value. 我试图使用findOneAndUpdate设置新字段及其值。 I had the settings "strict" set to false and "upsert" set to true. 我将设置“ strict”设置为false,将“ upsert”设置为true。 I saved it afterwards. 我后来保存了。 I did see that it was added in the Compass Community App, but when I tried to call the field later, it was undefined and I'm assuming because it isn't in the schema. 我确实看到它是在Compass社区应用程序中添加的,但是当我稍后尝试调用该字段时,它是未定义的,我假设是因为它不在架构中。 I wanted to know if there was a way around this or if I made any errors. 我想知道是否可以解决此问题,或者是否出错。

Users.findOneAndUpdate({
  userID: message.author.id
}, {
  $set: {
    "xp": xpToBeAdded,
    "level": 0
   }
}, {
  strict: false,
  upsert: true
}, async (err, userAdd) => {
  await userAdd.save().catch(err => console.log(err))
})

Original users schema: 原始用户架构:

const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
    userID: String,
    serverID: Array,
    userTag: String,
    username: String,
    dbCreatedAt: Number,
    dbCreatedAtDate: String
}, {strict: false})

module.exports = mongoose.model("User", userSchema)

If you are seeing the right values in compass, but it's not being returned, it's probably because your model doesn't know about those properties. 如果您在罗盘中看到了正确的值,但没有返回,则可能是因为您的模型不知道这些属性。 Try updating your model to this: 尝试将模型更新为此:

const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
    userID: String,
    xp: Number,
    level: Number,
    serverID: Array,
    userTag: String,
    username: String,
    dbCreatedAt: Number,
    dbCreatedAtDate: String
}, {strict: false})

module.exports = mongoose.model("User", userSchema)

Example of what is happening (notice how sex doesn't get logged, even though you're passing it in as a value): 发生的情况的示例(注意,即使您将性作为值传递,也不会记录sex ):

 class User { constructor(user) { this.name = user.name; this.age = user.age; } } const user1 = new User({name: 'Frank', age: 22, sex: 'M'}); console.log(user1); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向现有MongoDB文档添加字段(在Node.js中使用Mongoose) - Add a field to existing MongoDB document (with Mongoose in Node.js) 如何确定是否可以将新字段添加到现有对象? - How to be sure if a new field can be added to the pre-existing object or not? Chokidar Node.js package 从目录中的预先存在的文件中不必要地使用大量内存/RAM - Massive memory/RAM usage with Chokidar Node.js package from pre-existing files in directory being watched unnecessarily 回调(新的 MongoError(文档)); 没有连接到 MongoDB 数据库? (节点.js) - callback(new MongoError(document)); Not connecting to MongoDB Database? (Node.js) Node.js-MongoDB删除文档不起作用 - Node.js - MongoDB remove document doesn't work 仅当我的应用程序未托管在预先存在的路径上时,资产才能正确映射 - Assets only correctly map up when my application isn't hosted on a pre-existing path 如何在不使用write()的情况下将iframe文档替换为另一个现有的文档对象 - How to replace iframe document with another pre-existing document object, without using write() node.js如何在内部Mongodb中查找文档 - node.js how to find a document inside Inner Mongodb 如何检查Embedded Document中的元素是否为空? Node.js和Mongodb - How to check if elements in Embedded Document is empty or not? Node.js & Mongodb 如何使用node.js在MongoDB中查找下一个文档 - How to find the next document in MongoDB using node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM