如何将对象推送到MongoDB中另一个数组中的数组?

huangapple go评论100阅读模式
英文:

How to push object to array which is in another array in MongoDB?

问题

我有一个Mongoose模式:

  1. const UserSchema = new mongoose.Schema({
  2. mail: {
  3. type: String,
  4. required: true
  5. },
  6. password: {
  7. type: String,
  8. required: true
  9. },
  10. folders: [
  11. {
  12. folderName: {
  13. type: String,
  14. default: 'Main',
  15. required: true,
  16. },
  17. files: [
  18. {
  19. fileName: String,
  20. fileType: String,
  21. date: String,
  22. filePath: String,
  23. }
  24. ]
  25. }
  26. ]
  27. });

对于这个模式,我想从请求中获取用户的邮件和文件夹的名称,并将文件数据添加到这个文件夹中,但我不知道如何将文件推送到正确的文件夹中。

我想要实现这段代码:

  1. const mail = req.body.mail;
  2. const folderName = req.body.folderName;
  3. const file = {
  4. fileName: 'file222',
  5. fileType: '.png',
  6. date: '13-02-2022 21:15',
  7. filePath: '/uploads',
  8. }
  9. const user = await userModel.findOne({ mail: mail });
  10. const folders = user.folders.filter(folder => folder.folderName === folderName);
  11. folders[0].files.push(file);

但在MongoDB中,类似这样的:

  1. const userFile = await userModel.updateOne(
  2. { mail: mail, "folders.folderName": folderName },
  3. {
  4. $push: {
  5. folders: {
  6. files: file
  7. }
  8. }
  9. }
  10. );

但我不知道如何指定查询...我该怎么做?

英文:

I have a Mongoose Schema:

  1. const UserSchema = new mongoose.Schema({
  2. mail: {
  3. type: String,
  4. required: true
  5. },
  6. password: {
  7. type: String,
  8. required: true
  9. },
  10. folders: [
  11. {
  12. folderName: {
  13. type: String,
  14. default: 'Main',
  15. required: true,
  16. },
  17. files: [
  18. {
  19. fileName: String,
  20. fileType: String,
  21. date: String,
  22. filePath: String,
  23. }
  24. ]
  25. }
  26. ]
  27. });

For this scheme, I would like to get the user's mail and the name of the folder from the request and add the file data to this folder, but I don't know how to push file to correct folder.

I want to do this code:

  1. const mail = req.body.mail;
  2. const folderName = req.body.folderName;
  3. const file = {
  4. fileName: 'file222',
  5. fileType: '.png',
  6. date: '13-02-2022 21:15',
  7. filePath: '/uploads',
  8. }
  9. const user = await userModel.findOne({ mail: mail });
  10. const folders = user.folders.filter(folder => folder.folderName === folderName);
  11. folders[0].files.push(file);

but in MongoDB, something like this:

  1. const userFile = await userModel.updateOne(
  2. { mail: mail, "folders.folderName": folderName },
  3. {
  4. $push: {
  5. folders: {
  6. files: file
  7. }
  8. }
  9. }
  10. );

But I don't know how to specify that query...
how can I supposed to do it?

答案1

得分: 0

以下是翻译好的内容:

  1. const userFile = await userModel.updateOne(
  2. { mail: mail, "folders.folderName": folderName },
  3. {
  4. $push: {
  5. "folders.$.files": file
  6. }
  7. }
  8. );

文档
> 位置 $ 运算符标识要更新的数组中的一个元素,而不需要显式指定元素在数组中的位置。

英文:
  1. const userFile = await userModel.updateOne(
  2. { mail: mail, "folders.folderName": folderName },
  3. {
  4. $push: {
  5. folders.$.files: file
  6. }
  7. }
  8. );

Docs
> The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.

答案2

得分: 0

尝试一下:

  1. const userFile = await userModel.updateOne(
  2. { mail: mail, "folders.folderName": folderName },
  3. {
  4. $push: {
  5. "folders.$.files": file
  6. }
  7. }
  8. );

您可以在此处尝试更多:链接

英文:

Try it :

  1. const userFile = await userModel.updateOne(
  2. { mail: mail, "folders.folderName": folderName },
  3. {
  4. $push: {
  5. "folders.$.files":file
  6. }
  7. }
  8. );

You can try more here: LINK

huangapple
  • 本文由 发表于 2023年1月8日 01:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75042448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定