This is what I have in my db.
{
"_id": { "$oid" : "1111" },
"gallery": {
"images": [
{
"image": "test1.jpg",
"_id": { "$oid" : "1111a" }
},
{
"image": "test2.jpg",
"_id": { "$oid" : "1111b" }
},
{
"image": "test3.jpg",
"_id": { "$oid" : "1111c" }
}
]
}
}
,{
"_id": { "$oid" : "2222" },
"gallery": {
"images": [
{
"image": "test1.jpg",
"_id": { "$oid" : "2222a" }
}
]
}
}
I would like to update the second element (image:"test2.jpg") of the first element (_id:"1111").
The result should be
...
{
"image": "test2new.jpg",
"_id": { "$oid" : "1111b" }
},
...
I have the _id of the second image "1111b".
How can I do it?
UPDATE
I tried this query, but it doesn't work.
{
'_id':{'$oid' : '1111' }
,'gallery.image': $elemMatch:{ '_id':{'$oid':'1111b' } }
}
,{
$set:{ 'gallery.images.$.image':'test2new.jpg' }
}
SOLUTION
_id without quotes and without add { "$oid" :
{
{ _id : '1111' }
,{ 'gallery.image': $elemMatch:{ _id:'1111b' } }
}
,{
$set:{ 'gallery.images.$.image':'test2new.jpg' }
}
You can use the $ operator with update.Assuming your collection is named images you can use the following query:
db.images.update({'_id.oid':'1111','gallery.images':{'$elemMatch':{'image':'test2.jpg'}}},{'$set':{'gallery.images.$.image':'test2new.jpg'}})
You can read more about it here : http://docs.mongodb.org/manual/core/update/#update-a-document-element-without-specifying-its-position
Related
I have tried the following query but it was failing
db.getCollection('images').aggregate([
{
$project: {
"practiceId": "$practiceId",
"imageSize": { $binarySize: "$data" }
}
}])
And my sample data in mongodb is like
{
"_id" : ObjectId("60be3c9ea77d6312514276384"),
"_class" : "com.test.images.model.ImageEntity",
"practiceId" : NumberLong(242),
"fileName" : "img.jpg",
"contentType" : "image/jpeg",
"data" : { "$binary" : "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExMVFhUVFyAYFRcXFxsgIBggICggICAoHx8gJTAqICYxJR8fKjsrMTU3NTU1ICs7QDo1PzA1NjUBCgoKDg0OFxAQFjgdFhorNCsvKys3Ky0rN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RnO/S/wAkompVNrDclES9QndWuGw4YIHqea3IizaCIiD/2Q==", "$type" : "00" },
"thumbnail" : false
}
Any suggestions?
Maybe a problem with the data format. This one works:
db.images.insertOne({
"_class": "com.test.images.model.ImageEntity",
"practiceId": NumberLong(242),
"fileName": "img.jpg",
"contentType": "image/jpeg",
"thumbnail": false,
data: BinData(0, "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExMVFhUVFyAYFRcXFxsgIBggICggICAoHx8gJTAqICYxJR8fKjsrMTU3NTU1ICs7QDo1PzA1NjUBCgoKDg0OFxAQFjgdFhorNCsvKys3Ky0rNyszKzctLTMyLS03NzcrLS0rNzc3Kys4KzgrODgrKzg4NjctNy81MP/AABEIAMgAyAMBIgACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAABAUCAwYBB//EADwQAAEDAgQDBQUGBQQDAAAAAAEAAhEDIQQSMUEFUWEGEyJxgTKRobHBB0JSYtHwIzNy4fEUgpLSY6Ky/8QAGQEBAAMBAQAAAAAAAAAAAAAAAAECAwQF/8QAJBEBAQACAgICAgIDAAAAAAAAAAECAxEhEjEEUUFxMoETImH/2gAMAwEAAhEDEQA/APuKIiAiIgIiICIqHtjxr/S0C4HxOs3npJj9evoQce7U0cPLZDnjVo281wvFO11d4zF2UH2GNnTqOfn8FzArPdme+/i8UyZJHXXn7lDe9zjlFOTzMlZ3Jrjhz6WNLiFR7iS4Hq9x+WZTBxFtNwOhB2cSD6mY8tPgoVPhVVwAawNG8mPcBorXC9nrS8/5WN24xvj8fK/h0vZ/7QKfsVZtuTJHruPjzXc4TF06rc1NwcOYXxPF9nHgksI9LLoewfFXMf3TgQZ066e/4LTDZKy2abj7fU0WLTIWS2YCIiAiIgIiICIiAiIgIiICIiAiIgLgPtJYX1KbPuimXeuYAfvou/XJds8MCc3OnlHvn9+aipnt8yNIlvdxADpHMg/sKxwGGAiBCVmeMxoP8LdRqMbAe4BcW3m16ejxkW+F0hS6bFWYDjeFLsodJ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Ti/e4c0XumpQOW+pYfZ91x6Dms+J9mqTmnI57Z2zTHlmm3T3RoeMoYbF8NxTK0CpSnK8ssXNOog2nQi8SAq69OWN67jTZuwzx+q+uPCxLllTqBzQ5plrgC0jcG4QhbOZrL16Hr0tWJpogcjXSvMpWD6Z1BhShk52ysAFW4cEuAN4uVZquS2IiIqrCIiAiIgpOPs8TT0+X+VS1TdEWk9M8vbbh3K3olEUoSA4IXrxFCXmYr3MiKRm0BZQERQPHAQudpPaS6g8jUinO8/dP093JEVsUVa8EYKdPu5s1xyg7A3j3ypzqo5hEVb3Vp6a3Ylo3Wt2NaNwiJwjlpfxRnO/S/wAkompVNrDclES9QndWuGw4YIHqea3IizaCIiD/2Q==")
})
db.images.aggregate([
{
$project: {
"practiceId": "$practiceId",
"imageSize": { $binarySize: "$data" }
}
}
])
{
"practiceId" : 242,
"imageSize" : 4207.0
}
I insert these two documents:
db.images.insertMany([
{
author: "you",
data: {
"$binary": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUTExMVFhUVFyAYFRcXFxsgIBggICggICAoHx8gJTAqICYxJR8fKjsrMTU3NTU1ICs7QDo1PzA1NjUBCgoKDg0OFxAQFjgdFhorNCsvKys3Ky0rNyszKzctLTMyLS03NzcrLS0rNzc3Kys4KzgrODgrKzg4NjctNy81MP/AABEIAMgAyAMBIgACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAABAUCAwYBB//EADwQAAEDAgQDBQUGBQQDAAAAAAEAAhEDIQQSMUEFUWEGEyJxgTKRobHBB0JSYtHwIzNy4fEUgpLSY6Ky/8QAGQEBAAMBAQAAAAAAAAAAAAAAAAECAwQF/8QAJBEBAQACAgICAgIDAAAAAAAAAAECAxEhEjEEUUFxMoETImH/2gAMAwEAAhEDEQA/APuKIiAiIgIiICIqHtjxr/S0C4HxOs3npJj9evoQce7U0cPLZDnjVo281wvFO11d4zF2UH2GNnTqOfn8FzArPdme+/i8UyZJHXXn7lDe9zjlFOTzM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I5Ktq8IqscA9sTcEHX15rGrwMB3ftnvGtvoQ9tiWuBBBFrbgrmx35c8ZR1Z/Hw8ecatsRxRlNgfS11IbMEgG0Hc8tYgqJV7WV/Flp0wSbF2YgabAj5qyxWAaKbopZZgguFJsbw3u/aMayYtYqnxeFbroU3bMpej4+rCy+TpeB8YbiWFwGV7bVGTOUm4g7g7HzFiCFYFcBh8WMJVZXJimT3db+h2h/2ug+RcN19DIWurZ5Y/9Y7tfhl16YQoxwFK38NljI8I156a3N1KRa8SsubGLRGiZVkiIeJC8leyg8ZUgqaoTmqWw2HkoyTGSIiqsIiICxc2QQd7LJEHJ1W+006tK1YauWnop3FWZap5Ov8Aqq1wvC0jKugbMAjdZgkbKs4dii3wm42Vm2t0RLY2qD0PIrMDda5WUqEvK9IOEH06KIcCdnX6hTWr2VW4S+4vM7PVcLS4BWa5kurPNPxAOqNy5jNmkgFwkmATpHJZVXOmHNLTyc0ifKdfRdoCAtdVwcC1zQWnUESD5hVz1TJfXuuD55xwB1JzTur/AOzTi/e4c0XumpQOW+pYfZ91x6Dms+J9mqTmnI57Z2zTHlmm3T3RoeMoYbF8NxTK0CpSnK8ssXNOog2nQi8SAq69OWN67jTZuwzx+q+uPCxLllTqBzQ5plrgC0jcG4QhbOZrL16Hr0tWJpogcjXSvMpWD6Z1BhShk52ysAFW4cEuAN4uVZquS2IiIqrCIiAiIgpOPs8TT0+X+VS1TdEWk9M8vbbh3K3olEUoSA4IXrxFCXmYr3MiKRm0BZQERQPHAQudpPaS6g8jUinO8/dP093JEVsUVa8EYKdPu5s1xyg7A3j3ypzqo5hEVb3Vp6a3Ylo3Wt2NaNwiJwjlpfxRnO/S/wAkompVNrDclES9QndWuGw4YIHqea3IizaCIiD/2Q==")
}
])
The mongoexport looks like this:
{
"_id": { "$oid": "60f92b42607c82337782654e" },
"author": "you",
"data": {
"$binary": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAk...",
"$type": "00"
}
},
{
"_id": { "$oid": "60f92b42607c82337782654f" },
"author": "me",
"data": {
"$binary": {
"base64": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAA...",
"subType": "00"
}
}
}
Looks like mongo does not recognize it as Binary Data, the output is different.
I have following schema
{
question: String,
answer: Number,
option1: String,
option2: String,
option3: String,
option4: String,
subject: String,
chapter: String,
topic: String,
subtopic: String,
tags: { type: Array, default: [] },
difficulty: String,
media: { type: String, default: "" }
}
I want to create a index from this question schema, where a unique entry in index can be identified by tuple (subject, chapter, topic, subtopic)
{
"subject": {
"chapter": {
"topic": ["subtopics"],
"topic": ["subtopics"],
},
"chapter": {
"topic": ["subtopics"],
"topic": ["subtopics"],
},
},
"subject": {
"chapter": {
"topic": ["subtopics"],
"topic": ["subtopics"],
},
"chapter": {
"topic": ["subtopics"],
"topic": ["subtopics"],
},
},
}
To achieve above result I have been trying some aggregations and so far I am able to get upto
{
"subject": ["chapters"],
"subject": ["chapters"],
"subject": ["chapters"],
"subject": ["chapters"],
}
using following pipeline in mongoose
{
$group: {
_id: "$subject",
chapters: { $push: "$chapter" }
}
},
{
$group: {
_id: null,
chapters: { $push: { "k": "$_id", "v": "$chapters" } }
}
},
{
$replaceRoot: { "newRoot": { "$arrayToObject": "$chapters" } }
}
When I tried to extend my above logic to include topic ans subtopics using
{
$group: {
_id: "$subject",
chapters: { $push: { "k": "$chapter", "v": { topic: "$topic", subtopic: "$subtopic" } } }
}
},
{
$group: {
_id: null,
chapters: { $push: { "k": "$_id", "v": { "$arrayToObject": "$chapters" } } }
}
},
{
$replaceRoot: { "newRoot": { "$arrayToObject": "$chapters" } }
}
I get following output
{
"subject": {
"chapter": {
"topic": "value",
"subtopic": "value"
},
"chapter": {
"topic": "value",
"subtopic": "value"
},
},
"subject": {
"chapter": {
"topic": "value",
"subtopic": "value"
},
"chapter": {
"topic": "value",
"subtopic": "value"
},
},
}
Problem is that chapter is a object of last topic and subtopic only I know where I am making mistake but I don;t know how to fix it.
You can use this aggregation that mainly utilizes $objectToArray, the data restructuring is not so elegant though:
db.collection.aggregate(
[
{
"$group" : {
"_id" : {
"subject" : "$subject",
"chapter" : "$chapter",
"topic" : "$topic"
},
"topics" : {
"$push" : "$subtopic"
}
}
},
{
"$addFields" : {
"topic" : {
"k" : "$_id.topic",
"v" : "$topics"
}
}
},
{
"$group" : {
"_id" : {
"subject" : "$_id.subject",
"chapter" : "$_id.chapter"
},
"topics" : {
"$push" : "$topic"
}
}
},
{
"$addFields" : {
"topics" : {
"$arrayToObject" : "$topics"
}
}
},
{
"$addFields" : {
"chapters" : [
{
"k" : "$_id.chapter",
"v" : "$topics"
}
]
}
},
{
"$addFields" : {
"chapters" : {
"$arrayToObject" : "$chapters"
}
}
},
{
"$group" : {
"_id" : null,
"subject" : {
"$push" : {
"k" : "$_id.subject",
"v" : "$chapters"
}
}
}
},
{
"$project" : {
"final" : {
"$arrayToObject" : "$subject"
}
}
},
{
"$replaceRoot" : {
"newRoot" : "$final"
}
}
]);
Mongo query generated out of java code:
{
"pipeline": [{
"$match": {
"Id": "09cd9a5a-85c5-4948-808b-20a52d92381a"
}
},
{
"$group": {
"_id": "$result",
"id": {
"$first": "$result"
},
"labelKey": {
"$first": {
"$ifNull": ["$result",
"$result"]
}
},
"value": {
"$sum": 1
}
}
}]
}
Field 'result' can have values like Approved, Rejected, null and "" (empty string). What I am trying to achieve is combining the count of both null and empty together.
So that the empty string Id will have the count of both null and "", which is equal to 4
I'm sure theres a more "proper" way but this is what i could quickly come up with:
[
{
"$group" : {
"_id" : "$result",
"id" : {
"$first" : "$result"
},
"labelKey" : {
"$first" : {
"$ifNull" : [
"$result",
"$result"
]
}
},
"value" : {
"$sum" : 1.0
}
}
},
{
"$group" : {
"_id" : {
"$cond" : [{
$or: [
{"$eq": ["$_id", "Approved"]},
{"$eq": ["$_id", "Rejected"]},
]}},
"$_id",
""
]
},
"temp" : {
"$push" : {
"_id" : "$_id",
"labelKey" : "$labelKey"
}
},
"count" : {
"$sum" : "$value"
}
}
},
{
"$unwind" : "$temp"
},
{
"$project" : {
"_id" : "$temp._id",
"labelKey": "$temp.labelKey",
"count" : "$count"
}
}
],
);
Due to the fact the second group is only on 4 documents tops i don't feel too bad about doing this.
I have used $facet.
The MongoDB stage $facet lets you run several independent pipelines within the stage of a pipeline, all using the same data. This means that you can run several aggregations with the same preliminary stages, and successive stages.
var queries = [{
"$match": {
"Id": "09cd9a5a-85c5-4948-808b-20a52d92381a"
}
},{
$facet: {//
"empty": [
{
$match : {
result : { $in : ['',null]}
}
},{
"$group" : {
"_id" : null,
value : { $sum : 1}
}
}
],
"non_empty": [
{
$match : {
result : { $nin : ['',null]}
}
},{
"$group" : {
"_id" : '$result',
value : { $sum : 1}
}
}
]
}
},
{
$project: {
results: {
$concatArrays: [ "$empty", "$non_empty" ]
}
}
}];
Output :
{
"results": [{
"_id": null,
"value": 52 // count of both '' and null.
}, {
"_id": "Approved",
"value": 83
}, {
"_id": "Rejected",
"value": 3661
}]
}
Changing the group by like below solved the problem
{
"$group": {
"_id": {
"$ifNull": ["$result", ""]
},
"id": {
"$first": "$result"
},
"labelKey": {
"$first": {
"$ifNull": ["$result",
"$result"]
}
},
"value": {
"$sum": 1
}
}
}
Json Structure:
"_id" : ObjectId("55d6cb28725f3019a5241781"),
"Number" : {
"value" : "1234567",
},
"DeviceID" : {
"value" : "01",
}
"type" : {
"value" : "ce06"}
Now i want to find only those keys document which start from /dev/.
i tried this script:
var cur = db.LIVEDATA.find({"ProductIMEIno.value":"359983007488004"});
cur.forEach(function(doc){
var keynames = Object.keys(doc);
print('the length is '+keynames.length);
for(var i=0;i<keynames.length;i++){
if(keynames[i].match(/Dev/)){
print("the name is "+keynames); }}} )
but this is not working properly.
Desired Output;
Only this document should show on the basis of key name search.
"DeviceID" : {
"value" : "01",
MongoDB isn't designed to find keys dynamically like this; it's much easier to use it to find values dynamically, so you could restructure your data structure to allow this:
"_id" : ObjectId("55d6cb28725f3019a5241781"),
"data" : [
{
"key" : "Number",
"value" : "1234567",
},
{
"key": "DeviceID",
"value" : "01",
},
{
"key" : "type",
"value" : "ce06"
}
]
Then you will be able to query it like this:
db.LIVEDATA.aggregate([
{$match: {"ProductIMEIno.value":"359983007488004"}},
{$unwind: "$data"},
{$match: {"data.key" : /^dev/i }}
]);
That will return data structured like this:
{
"_id" : ObjectId("55d6cb28725f3019a5241781"),
"data" : {
"key" : "DeviceID",
"value" : "01"
}
}
Suppose you have a data collection like this:
[
{
"Number": {
"value": "1234567"
},
"DeviceID": {
"value": "01"
},
"DeviceID2": {
"value": "01",
"name": "abc123"
},
"type": {
"value": "ce06"
}
},
{
"Number": {
"value": "1234568"
},
"DeviceID": {
"value": "02"
},
"type": {
"value": "ce07"
}
}
]
You can use following aggregation:
db.collection.aggregate([
{
"$match": {}
},
{
"$addFields": {
"root_key_value_list": {
"$objectToArray": "$$ROOT"
}
}
},
{
"$unwind": "$root_key_value_list"
},
{
"$match": {
"root_key_value_list.k": {
"$regex": "^Dev"
}
}
},
{
"$group": {
"_id": "$_id",
"root_key_value_list": {
"$push": "$root_key_value_list"
}
}
},
{
"$project": {
"root": {
"$arrayToObject": "$root_key_value_list"
}
}
},
{
"$replaceRoot": {
"newRoot": "$root"
}
}
])
the result will be:
[
{
"DeviceID": {
"value": "01"
},
"DeviceID2": {
"name": "abc123",
"value": "01"
}
},
{
"DeviceID": {
"value": "02"
}
}
]
playground:
https://mongoplayground.net/p/z5EeHALCqzy
say I have these two documents:
{
"_id":"sampleA",
"value":{
"data":[
{
"thing":"A"
},
{
"thing":"B"
},
{
"thing":"C"
},
{
"thing":"D"
},
{
"thing":"E"
}
]
}
}
{
"_id":"sampleB",
"value":{
"data":[
{
"thing":"C"
},
{
"thing":"D"
},
{
"thing":"E"
},
{
"thing":"F"
}
]
}
}
and I want to group them into one document, retaining the label of "sampleA" or "sampleB" such as
{
"_id": null,
"sampleA": [
{
"thing": "A"
},
{
"thing": "B"
},
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
}
],
"sampleB": [
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
},
{
"thing": "F"
}
]
}
So that way I can use the set intersection operator. How do I go about doing this? I tried:
db.testz.aggregate(
[{
$match: {
_id: {
$in: ["sampleA", "sampleB"]
}
}
}, {
'$group': {
_id: null,
a: {
$push: "$value"
}
}
}]
);
which gives me
{
"_id": null,
"a": [
{
"data": [
{
"thing": "A"
},
{
"thing": "B"
},
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
}
]
},
{
"data": [
{
"thing": "C"
},
{
"thing": "D"
},
{
"thing": "E"
},
{
"thing": "F"
}
]
}
]
}
Presumably I could then use the set intersection operator if I could index the items in a?
db.testz.aggregate(
[{
$match: {
_id: {
$in: ["sampleA", "sampleB"]
}
}
}, {
'$group': {
_id: null,
a: {
$push: "$value"
}
}
}, {
'$project': {
int: {
$setIntersection: ["$a.0", "$a.1"]
}
}
}]
);
^^ obviously this last step here doesn't work, but I am trying to illustrate the point.
I think the only way to do this presently (MongoDB 2.6) is to unwind the arrays and then recollect in a set:
> db.testz.aggregate([
{ "$match" : { "_id" : { "$in" : ["sampleA", "sampleB"] } } },
{ "$unwind" : "$value.data" },
{ "$group" : { "_id" : 0, "intersection" : { "$addToSet" : "$value.data" } } }
])
It's not an efficient way to do it, but it gets the job done. I was probing you for more specific info to see if there was some way to avoid this answer :(