首页 > 解决方案 > mongodb 将字符串从一个数组移动到另一个数组

问题描述

我需要请求将字符串从一个数组移动到另一个数组。我是否需要从一个数组中删除一个字符串,然后将其推送到另一个数组中,或者是否有可能以某种不同的方式?我怎样才能做到这一点 ?

JSON:https ://jsoneditoronline.org/?id=442f1dae0b2d4997ac69d44614e55aa6 .................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ...... 在此处输入图像描述

{
   "_id":"f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
   "bio":{
      "firstname":"Лена",
      "lastname":"фыв",
      "middlename":"",
      "company":"вв"
   },
   "files":[
      {
         "_id":"2e4e40c7-4df6-4974-8d16-bb24cd8134d6",
         "destination":"./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
         "filename":"2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
         "path":"uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
         "folder":"f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
         "info":{
            "size":20805727,
            "mimetype":"audio/mp3",
            "encoding":"7bit",
            "originalname":"Ахуевший Ленусик (Банк русский стандарт). Выпуск #5..mp3",
            "fieldname":"selectedFile"
         },
         "userId":"5e05da745b21e61ccc84a892",
         "date":"2019-12-27T10:19:12.213Z",
         "guessId":{
            "f58482b1-ae3a-4d8a-b53b-ede80fe1e225":[
               "5e05da745b21e61ccc84a892"
            ],
            "b7d00dea-c872-43f4-b193-8454bef5cf85":[
               "5e094d988ddbe02020e13879"
            ]
         }
      },
      {
         "_id":"81b94dea-ece6-421c-b68a-0aa59332cd0d",
         "destination":"./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
         "filename":"81b94dea-ece6-421c-b68a-0aa59332cd0d.mp3",
         "path":"uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\81b94dea-ece6-421c-b68a-0aa59332cd0d.mp3",
         "folder":"f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
         "info":{
            "size":13515683,
            "mimetype":"audio/mp3",
            "encoding":"7bit",
            "originalname":"Выпуск #75 Попрошайка НСВ..mp3",
            "fieldname":"selectedFile"
         },
         "userId":"5e05da745b21e61ccc84a892",
         "date":"2019-12-27T10:25:37.710Z",
         "guessId":{
            "b7d00dea-c872-43f4-b193-8454bef5cf85":[
               "5e05da745b21e61ccc84a892"
            ]
         }
      }
   ],
   "date":"2019-12-27T10:19:12.213Z",
   "__v":1
}

架构

  const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const HabalkaSchema = new Schema({
  _id: {
    type: String
  },
  bio: {
    firstname: String,
    lastname: String,
    middlename: String,
    company: String
  },
  files: [
    {
      _id: {
        type: String
      },
      destination: {
        type: String
      },
      filename: {
        type: String
      },
      path: {
        type: String
      },
      folder: {
        type: String
      },
      info: {
        size: {
          type: Number
        },
        mimetype: {
          type: String
        },
        encoding: {
          type: String
        },
        originalname: {
          type: String
        },
        fieldname: {
          type: String
        },
      },
      date: {
        type: Date,
        default: Date.now
      },
      bio: {
        type: Object
      },
      userId: String,
      guessId: {},
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Habalka = mongoose.model('habalka', HabalkaSchema);

标签: node.jsmongodbexpressmongoose

解决方案


您可以使用$push$pull的组合以及$ 位置运算符来指示files应该修改哪个文档:

db.col.update(
    { _id: "f58482b1-ae3a-4d8a-b53b-ede80fe1e225", "files._id": "2e4e40c7-4df6-4974-8d16-bb24cd8134d6" },
    { 
        $push: {  "files.$.guessId.f58482b1-ae3a-4d8a-b53b-ede80fe1e225": "5e094d988ddbe02020e13879" },
        $pull: {  "files.$.guessId.b7d00dea-c872-43f4-b193-8454bef5cf85": "5e094d988ddbe02020e13879" },
    }
)

推荐阅读