Reputation: 1398
I need to make a request to move a string from one array to another. Do I need to delete a string from one array and then push it into another, or is it possible somehow differently? How can I do that ?
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
}
Schema
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);
Upvotes: 1
Views: 57
Reputation: 49945
You can use a combination of $push and $pull along with $ positional operator to indicate which document under files
should be modified:
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" },
}
)
Upvotes: 1