I am trying to get data from Mongo DB by filtering a nested object.
the collection structure is :
{
"id":"8820624457",
"type":"CreateEvent",
"actor":{
"id":27394937,
"login":"Johnson0608",
"display_login":"Johnson0608",
"gravatar_id":"",
"url":"https://api.github.com/users/Johnson0608",
"avatar_url":"https://avatars.githubusercontent.com/u/27394937?"
},
"repo":{
"id":163744671,
"name":"Johnson0608/test",
"url":"https://api.github.com/repos/Johnson0608/test"
},
"payload":{
"ref":"master",
"ref_type":"branch",
"master_branch":"master",
"description":null,
"pusher_type":"user"
},
"public":true,
"created_at":"2019-01-01T15:00:00Z"
}
I am trying to get data by repo id.
my code is :
collection.find({'repo.id':id}).toArray(function(err, docs) {
console.log(id);
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
res.status(200).json({docs});
callback(docs);
});
but I am getting empty array, would be grateful is someone can point me to the right direction
MongoDB compares types before values. If your id comes from req.params it's probably passed as string while repo.id seems to be a number. Try to convert your value to number:
const id = +req.params.repoId
I am storing in my mongodb collection the video id of a video served in the cloud. When my front end makes an API call for that video, I want to return the formatted URL based on that video id. I have a function that does this, but I can't get it to work with $addFields in and aggregate pipeline:
My documents look like this:
{
"date" : ISODate("2018-03-30T00:00:00.000+0000"),
"title" : "Tips and Tricks",
"video_id" : "13966740",
}
...and I want my API call to return them with an extra field, "image_url":
{
"date" : ISODate("2018-03-30T00:00:00.000+0000"),
"title" : "Tips and Tricks",
"video_id" : "13966740",
"image_url" : "https://myhostingservice.com/13966740/preview_image",
}
This is what I'm trying in models/video.js:
const hostingservice = require('myhostingservicehelperfunctions');
module.exports.getVideo = function (callback) {
videoCollection.aggregate(
[
{ $match: { } },
{ $addFields: {
image_url: hostingservice.createImageURL("$video_id")
}
},
{ $sort: {
'date' : -1 }
}
],
callback);
};
The helper function simply takes the string param and returns a string.
myhostingservicehelperfunctions.js:
module.exports.createImageURL = function ImageURL(video_id){
return 'https://myhostingservice.com/' + video_id + '/preview_image';
};
My front end receives all the correct data, but the value of image_url is "https://myhostingservice.com/$video_id/preview_image", which makes me think my function ran, but it was passed the actual string "$video_id", not the value of the key, video_id. Look closely, the URL contains "$video_id", not "13966740" from the example above.
What am I doing wrong here?
You can't concatenate strings like that in mongoDB aggregation, you'll need to concatenate strings using the concat operator, so the function "createImageURL" should look something like this,
module.exports.createImageURL = function ImageURL(){
return { $concat: [ "https://myhostingservice.com/", "$video_id", "/preview_image" ] };
};
schema
images:{ type : Array , "default" : [] }
controller
return new Promise(function(resolve,reject){
var dt = new Date(req.body.dateOfBirth)
var experiences = new Experiences({
'images': req.body.images
});
experiences.save(function(err,experiences){
if(err){
reject(err);
}else{
resolve(experiences);
}
});
});
I need to save more than one image into single image
posting Data as :
"https://images.pexels.com/photos/346768/pexels-photo-346768.jpeg?w=940&h=650&auto=compress&cs=tinysrgb","https://images.pexels.com/photos/287240/pexels-photo-287240.jpeg?w=940&h=650&auto=compress&cs=tinysrgb"
but when i viewing my document. getting result like this :
"images" : [
"\"https://images.pexels.com/photos/346768/pexels-photo-346768.jpeg?w=940&h=650&auto=compress&cs=tinysrgb\",\"https://images.pexels.com/photos/287240/pexels-photo-287240.jpeg?w=940&h=650&auto=compress&cs=tinysrgb\""
]
what am i doing wrong please help?
i want result like:
"images" : [
"https://images.pexels.com/photos/346768/pexels-photo-346768.jpeg?w=940&h=650&auto=compress&cs=tinysrgb","https://images.pexels.com/photos/287240/pexels-photo-287240.jpeg?w=940&h=650&auto=compress&cs=tinysrgb"
]
My guess is that req.body.images is stringified. I believe it is a string containing many values, not an array. When you post that to MongoDB, since you specified that images must be an array, it saves it as an array of strigified values.
So, blind guess, but I believe you are posting (client-side) something like :
$.post("/save", { images : JSON.stringify(imagesArray) } )
when you should just post your array as is :
$.post("/save", { images : imagesArray} )
But it's hard to know for sure, because we don't have the front-end code or even console.log(req.body.images)
In the following example, assume the document is in the db.people collection.
How to remove the 3rd element of the interests array by it's index?
{
"_id" : ObjectId("4d1cb5de451600000000497a"),
"name" : "dannie",
"interests" : [
"guitar",
"programming",
"gadgets",
"reading"
]
}
This is my current solution:
var interests = db.people.findOne({"name":"dannie"}).interests;
interests.splice(2,1)
db.people.update({"name":"dannie"}, {"$set" : {"interests" : interests}});
Is there a more direct way?
There is no straight way of pulling/removing by array index. In fact, this is an open issue http://jira.mongodb.org/browse/SERVER-1014 , you may vote for it.
The workaround is using $unset and then $pull:
db.lists.update({}, {$unset : {"interests.3" : 1 }})
db.lists.update({}, {$pull : {"interests" : null}})
Update: as mentioned in some of the comments this approach is not atomic and can cause some race conditions if other clients read and/or write between the two operations. If we need the operation to be atomic, we could:
Read the document from the database
Update the document and remove the item in the array
Replace the document in the database. To ensure the document has not changed since we read it, we can use the update if current pattern described in the mongo docs
You can use $pull modifier of update operation for removing a particular element in an array. In case you provided a query will look like this:
db.people.update({"name":"dannie"}, {'$pull': {"interests": "guitar"}})
Also, you may consider using $pullAll for removing all occurrences. More about this on the official documentation page - http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull
This doesn't use index as a criteria for removing an element, but still might help in cases similar to yours. IMO, using indexes for addressing elements inside an array is not very reliable since mongodb isn't consistent on an elements order as fas as I know.
in Mongodb 4.2 you can do this:
db.example.update({}, [
{$set: {field: {
$concatArrays: [
{$slice: ["$field", P]},
{$slice: ["$field", {$add: [1, P]}, {$size: "$field"}]}
]
}}}
]);
P is the index of element you want to remove from array.
If you want to remove from P till end:
db.example.update({}, [
{ $set: { field: { $slice: ["$field", 1] } } },
]);
Starting in Mongo 4.4, the $function aggregation operator allows applying a custom javascript function to implement behaviour not supported by the MongoDB Query Language.
For instance, in order to update an array by removing an element at a given index:
// { "name": "dannie", "interests": ["guitar", "programming", "gadgets", "reading"] }
db.collection.update(
{ "name": "dannie" },
[{ $set:
{ "interests":
{ $function: {
body: function(interests) { interests.splice(2, 1); return interests; },
args: ["$interests"],
lang: "js"
}}
}
}]
)
// { "name": "dannie", "interests": ["guitar", "programming", "reading"] }
$function takes 3 parameters:
body, which is the function to apply, whose parameter is the array to modify. The function here simply consists in using splice to remove 1 element at index 2.
args, which contains the fields from the record that the body function takes as parameter. In our case "$interests".
lang, which is the language in which the body function is written. Only js is currently available.
Rather than using the unset (as in the accepted answer), I solve this by setting the field to a unique value (i.e. not NULL) and then immediately pulling that value. A little safer from an asynch perspective. Here is the code:
var update = {};
var key = "ToBePulled_"+ new Date().toString();
update['feedback.'+index] = key;
Venues.update(venueId, {$set: update});
return Venues.update(venueId, {$pull: {feedback: key}});
Hopefully mongo will address this, perhaps by extending the $position modifier to support $pull as well as $push.
I would recommend using a GUID (I tend to use ObjectID) field, or an auto-incrementing field for each sub-document in the array.
With this GUID it is easy to issue a $pull and be sure that the correct one will be pulled. Same goes for other array operations.
For people who are searching an answer using mongoose with nodejs. This is how I do it.
exports.deletePregunta = function (req, res) {
let codTest = req.params.tCodigo;
let indexPregunta = req.body.pregunta; // the index that come from frontend
let inPregunta = `tPreguntas.0.pregunta.${indexPregunta}`; // my field in my db
let inOpciones = `tPreguntas.0.opciones.${indexPregunta}`; // my other field in my db
let inTipo = `tPreguntas.0.tipo.${indexPregunta}`; // my other field in my db
Test.findOneAndUpdate({ tCodigo: codTest },
{
'$unset': {
[inPregunta]: 1, // put the field with []
[inOpciones]: 1,
[inTipo]: 1
}
}).then(()=>{
Test.findOneAndUpdate({ tCodigo: codTest }, {
'$pull': {
'tPreguntas.0.pregunta': null,
'tPreguntas.0.opciones': null,
'tPreguntas.0.tipo': null
}
}).then(testModificado => {
if (!testModificado) {
res.status(404).send({ accion: 'deletePregunta', message: 'No se ha podido borrar esa pregunta ' });
} else {
res.status(200).send({ accion: 'deletePregunta', message: 'Pregunta borrada correctamente' });
}
})}).catch(err => { res.status(500).send({ accion: 'deletePregunta', message: 'error en la base de datos ' + err }); });
}
I can rewrite this answer if it dont understand very well, but I think is okay.
Hope this help you, I lost a lot of time facing this issue.
It is little bit late but some may find it useful who are using robo3t-
db.getCollection('people').update(
{"name":"dannie"},
{ $pull:
{
interests: "guitar" // you can change value to
}
},
{ multi: true }
);
If you have values something like -
property: [
{
"key" : "key1",
"value" : "value 1"
},
{
"key" : "key2",
"value" : "value 2"
},
{
"key" : "key3",
"value" : "value 3"
}
]
and you want to delete a record where the key is key3 then you can use something -
db.getCollection('people').update(
{"name":"dannie"},
{ $pull:
{
property: { key: "key3"} // you can change value to
}
},
{ multi: true }
);
The same goes for the nested property.
this can be done using $pop operator,
db.getCollection('collection_name').updateOne( {}, {$pop: {"path_to_array_object":1}})
I have an array inside a collection in mongodb as per below.
{
"_id" : ObjectId("53dbb05fa976627439d43884"),
"employee" : [
{
"date" : "1986-03-10"
},
{
"date" : "1986-12-11"
}
]
}
Now I want to find all distinct date and output it to array in nodejs format.
var collection = db.collection('employee');
collection.distinct('employee.date').toArray(function(err, docsa) {
console.log(docsa);
});
};
The problem is I will receive the following error, but those collection.distinct command can be run on mongodb.Anyhow I can avoid the error ?
TypeError: Cannot call method 'toArray' of undefined
Or is there any way I can use collection.find() to get the same output?
you try collection.distinct('employee.data'), on a collection called 'employee'. I believe you want :
collection.distinct('date')