In mongodb is it possible to query by sub-object? - mongodb

I have an doc in mongo:
{
"_id" : ObjectId("54eb5189ad9685bbb622ca52"),
"header" : {
"title" : "Project Name 1",
"header_img" : "project_name_1.png",
"project_stats" : {
"sqFt" : 20000,
"tons" : 300,
"duration" : "6 months",
"type" : "education facility",
"summary" : "Give quick summary of problem solved."
}
},
"row_project_detail" : {
"project_logo" : "project_name_1_logo.png",
"header" : "Project Name 1 was a project where...",
"paragraph" : "blah blah blah blah"
},
"row_1" : {
"img" : "project_name_2.png"
},
"row_2" : {
"img_1" : "project_name_3.png",
"img_2" : "project_name_4.png"
},
"row_3" : {
"img" : "project_name_5.png"
},
"row_4" : {
"img" : "project_name_6.png"
}
}
I have tried to query by db.projects.find({ header: { title: "Project Name 1"} }); but it does not produce results. How can I query by the title key in the header object? Is this possible or do I simply need to duplicate the title key from the header sub-object and put that at the root of the doc?

Use dot notation:
db.projects.find({ "header.title": "Project Name 1"});

Related

Mongodb update and delete operations in a single query

I have documents in which I would like to update the hostUser with one of the members of the document,also have to delete the record from the member document and add the chips of the deleted member in the club chips.
Here is the sample document.
{
"_id" : "1002",
"hostUser" : "1111111111",
"clubChips" : 10000,
"requests" : {},
"profile" : {
"clubname" : "AAAAA",
"image" : "0"
},
"tables" : [
"SJCA3S0Wm"
],
"isDeleted" : false,
"members" : {
"1111111111" : {
"chips" : 0,
"id" : "1111111111"
},
"2222222222" : {
"chips" : 0,
"id" : "2222222222"
}
}
}
This is what I have tried.
db.getCollection('test').updateMany({"hostUser":"1111111111"},
{"$set":{"hostUser":"2222222222"},"$unset":{"members.1111111111":""}})
This is how you would handle unset and set in a single call to updateMany. Can you please clarify what you meant by "check if the values exist in the member field"?
db.getCollection('test').updateMany(
{"hostUser":"1111111111"},
{
'$set': {"hostUser":"2222222222"} ,
'$unset': {"members.1111111111":""}
}
)

Spring data mongo remove element in sub collection

I have this document:
{
"_id" : ObjectId("5a698b8bc2539428e3738c7d"),
"_class" : "com.xxx.xxx.inf.dao.mongo.domain.IndexDO",
"product_id" : "5a67fde8c2539414b9712482",
"cn_name" : "index - test",
"en_name" : "test index - test",
"sample_desc" : "this is a sample description",
"people_type" : 1,
"ref_literature" : [
{
"_class" : "com.xxx.xxx.inf.dao.mongo.domain.RefLiterature",
"_id" : null,
"name" : "ref 1"
},
{
"_class" : "com.xxx.xxx.inf.dao.mongo.domain.RefLiterature",
"_id" : null,
"name" : "ref 2"
}
],
"user_id" : 6,
"gmt_create" : ISODate("2018-01-25T07:47:23.800+0000"),
"detail_desc" : null,
"gmt_modified" : ISODate("2018-01-25T07:49:50.923+0000"),
"index_level" : [
{
"_class" : "com.xxx.xxx.inf.dao.mongo.domain.IndexLevel",
"_id" : ObjectId("5a72c83050abd32e7e54340d"),
"up_val" : "3.2",
"down_val" : "5.7",
"calc_symbol_type" : 2,
"level_desc" : "this is a test level desc"
}
]
}
I want to delete the element in index_level, and in here is:
{
"_class" : "com.xxx.xxx.inf.dao.mongo.domain.IndexLevel",
"_id" : ObjectId("5a72c83050abd32e7e54340d"),
"up_val" : "3.2",
"down_val" : "5.7",
"calc_symbol_type" : 2,
"level_desc" : "this is a test level desc"
}
I can remove this sub document in mongo shell with the command:
db.index.update(
{"_id":ObjectId("5a698b8bc2539428e3738c7d")},
{$pull:{"index_level": {"_id":ObjectId("5a72c83050abd32e7e54340d")}}}
)
But when I removed use mongotemplate code in below, I was failed to do this:
mongoTemplate.updateFirst(
Query(Criteria.where("_id").`is`(ObjectId("5a698b8bc2539428e3738c7d"))),
Update().pull("index_level", Query(Criteria.where("_id").`is`(ObjectId("5a72c83050abd32e7e54340d")))),
IndexDO::class.java
)
and tried below code:
mongoTemplate.updateFirst(
Query(Criteria.where("_id").`is`(ObjectId("5a698b8bc2539428e3738c7d")).and("index_level._id").`is`(ObjectId("5a72c83050abd32e7e54340d"))),
Update().pull("index_level", Query(Criteria.where("_id").`is`(ObjectId("5a72c83050abd32e7e54340d")))),
IndexDO::class.java
)
and finally, I can remove this sub document with this code:
mongoTemplate.updateFirst(
Query(Criteria.where("index_level._id").`is`(ObjectId("5a72c83050abd32e7e54340d"))),
Update().pull("index_level", Query(Criteria.where("_id").`is`(ObjectId("5a72c83050abd32e7e54340d")))),
IndexDO::class.java)
But this code has a problem, because in here the position field is _id, when I change the position field like "calc_symbol_type", the final code will remove all calc_symbol_type equals to the value which I send to the method.
So the question is where I was wrong? and the mongo shell command converts to mongotemplate code will be what?
Thanks !

Why does MapReduce return undefined for a field that exists in document?

I am trying to debug a strange issue I am running into while running mapreduce on a collection:
For reference, here's a single document from the collection:
{
"_id" : "ITOUXFWgvWs",
"source" : "youtube",
"insert_datetime" : ISODate("2017-04-06T22:27:43.598Z"),
"processed" : false,
"raw" : {
"id" : "ITOUXFWgvWs",
"etag" : "\"m2yskBQFythfE4irbTIeOgYYfBU/hiQtS6aptLlqxTpsYp1EJIRcoZo\"",
"snippet" : {
"publishedAt" : ISODate("2017-04-06T13:25:28Z"),
"title" : "Alarm.com: The Only Smart Home App You Need",
"channelId" : "UC_HZfoZUP36STk7SrtKYH4g",
"description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home? It can all come together with Alarm.com.",
"categoryId" : "28",
"channelTitle" : "Alarm.com",
"thumbnails" : {
"default" : {
"height" : 90,
"width" : 120,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/default.jpg"
},
"standard" : {
"height" : 480,
"width" : 640,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/sddefault.jpg"
},
"high" : {
"height" : 360,
"width" : 480,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/hqdefault.jpg"
},
"medium" : {
"height" : 180,
"width" : 320,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/mqdefault.jpg"
},
"maxres" : {
"height" : 720,
"width" : 1280,
"url" : "https://i.ytimg.com/vi/ITOUXFWgvWs/maxresdefault.jpg"
}
},
"liveBroadcastContent" : "none",
"localized" : {
"title" : "Alarm.com: The Only Smart Home App You Need",
"description" : "All these new connected devices are awesome, but wouldn’t it be great if you could use one app for the entire connected home? It can all come together with Alarm.com."
}
},
"contentDetails" : {
"duration" : "PT37S",
"dimension" : "2d",
"definition" : "hd",
"licensedContent" : false,
"projection" : "rectangular",
"caption" : "false"
},
"kind" : "youtube#video",
"statistics" : {
"likeCount" : "0",
"dislikeCount" : "0",
"favoriteCount" : "0",
"viewCount" : "32"
},
"uploaded" : ISODate("2017-04-06T13:25:28Z")
},
}
I am literally following the mapreduce debug steps from official mongo documentation.
Here's what my mapreduce script looks like:
var map = function() {
emit("1", this._id);
};
var emit = function(key, value) {
print("emit");
print("key: " + key + " value: " + tojson(value));
}
var myDoc = db.getCollection("abc").find({}).limit(1);
map.apply(myDoc);
And it always produces the result like this:
MongoDB shell version: 2.4.6
connecting to: test
emit
key: 1 value: undefined
I expect the script to emit the _id since it clearly exists in the document but it doesn't.
What might be possible cause of this?
find() always returns a cursor.
Replace it with findOne()
var myDoc = db.getCollection("abc").findOne({});
Or store the documents in an Array using toArray()
var myDoc = db.getCollection("abc").find({}).limit(1).toArray()[0];

How to publish only unique documents in Meteor?

So I have mongodb collection with documents that some of them have the same key (beatmapset_id):
{
"_id" : "fgq7SK7LnLbcFzgSb",
"beatmapset_id" : "59049",
"version" : "Hard",
"artist" : "UNDEAD CORPORATION",
"title" : "Yoru Naku Usagi wa Yume wo Miru",
"creator" : "Strawberry"
}
{
"_id" : "u7M8wibPwxuStNLSM",
"beatmapset_id" : "59049",
"version" : "jom's Taiko Muzukashii",
"artist" : "UNDEAD CORPORATION",
"title" : "Yoru Naku Usagi wa Yume wo Miru",
"creator" : "Strawberry"
}
{
"_id" : "ChRwdj7kbPL7oYgty",
"beatmapset_id" : "59049",
"version" : "jom's Taiko Oni",
"artist" : "UNDEAD CORPORATION",
"title" : "Yoru Naku Usagi wa Yume wo Miru",
"creator" : "Strawberry"
}
How do I publish only unique documents? Something like this:
Meteor.publish("beatmaps", function() {
return Beatmaps.find({ beatmapset_id: 1 }, { unique: true });
});
Now i solve this having 2 collections. One with all documents and one where I removed those documents with this code:
db.beatmaps.ensureIndex({ beatmapset_id: 1 }, { unique: true, dropDups: true });
But it's slow and when my first collection updates I also need to update second as well. I'm sure it can be done more consistent way.

Select entries which doesn't have specified key

Assume, that I have bunch of entries in a document:
db.document
and some of them doesn't have some key, let's say name. So we have two types of entries - with and without name.
{ "_id" : ObjectId("4dea81a8bd2bb0323800002d"), "fetched_at" : ISODate("2013-08-02T17:41:30Z"), "keyword" : "110770", "name" : "SOME NAME" }
{ "_id" : ObjectId("4dea81a8bd2bb0323800002a"), "fetched_at" : ISODate("2013-08-02T17:44:17Z"), "keyword" : "125176" }
I want to remove all entries without name property, because it makes my database incosistent. How can I do that? I tried with null and undefined but it doesn't works.
it's possible using $exists:
db.document.remove( { name : { $exists : false } } );
db.document.remove( { name : null } )
should work too. Example:
> db.document.insert({"fetched_at" : ISODate("2013-08-02T17:41:30Z"), "keyword" : "110770", "name" : "SOME NAME"})
> db.document.insert({"fetched_at" : ISODate("2013-08-02T17:44:17Z"), "keyword" : "125176" })
> db.document.insert({"fetched_at" : ISODate("2013-08-02T17:44:17Z"), "keyword" : "125176" })
> db.document.find().size()
3
> db.document.remove({name:null})
> db.document.find().size()
1
> db.document.find().pretty()
{
"_id" : ObjectId("520eac7e5d0ee1aa8515a550"),
"fetched_at" : ISODate("2013-08-02T17:41:30Z"),
"keyword" : "110770",
"name" : "SOME NAME"
}