update multiple documents with sub array elements in mongodb without $ operator - mongodb

I want to update the element of subarray with another element of same array index. Actually what mistake what I have done is typo mistake in column name (reviews, review) so want to update all reviews column with review. Can anybody give me the query to update this scenerio.
This is my document json
{
"_id" : ObjectId("57a5df273c6c00d1378b456d"),
"review_time" : ISODate("2016-02-06T12:59:19.000Z"),
"review" : "My name is Sandra P. and I am a satisfied customer. I've been working with eBrandz for a little more than two years and plan on keep doing so. They are always there whenever I have any concern and their approach is very professional. I would definitely recommend their services as I have seen great results.",
"rating" : 5,
"review_reply" : [
{
"author" : "http://new york.cylex usa.com/company/ebrandz inc 18441959.html",
"reply_time" : "1969-12-31",
"reviews" : "Sandra thanks for recommending ebrandz, appreciated."
},
{
"author" : "http://new york.cylex usa.com/company/ebrandz inc 18441959.html",
"reply_time" : "1969-12-31",
"reviews" : "Sandra thanks for recommending ebrandz, appreciated.1"
},
{
"author" : "http://new york.cylex usa.com/company/ebrandz inc 18441959.html",
"reply_time" : "1969-12-31",
"review" : "Sandra thanks for recommending ebrandz, appreciated22."
}
],
}

db.temp_reviews.find( {'review_reply.reviews' : {$exists : true} } ).forEach( function (doc) {
doc.review_reply.forEach(function (z) {
if(z.reviews){
z.review = z.reviews
delete z.reviews;
}
});
db.temp_reviews.save(doc);
});

Your were close, you just need to loop through your array, check if wrong field exist, create the field, delete the wrong field :
db.reviews.find({
'review_reply.reviews': {
$exists: true
}
}).forEach(function(doc) {
for (var i = 0; i < doc.review_reply.length; i++) {
if (doc.review_reply[i].hasOwnProperty("reviews")) {
doc.review_reply[i].review = doc.review_reply[i].reviews;
delete doc.review_reply[i].reviews;
}
}
db.reviews.save(doc);
});

Related

How to return specific field's value in mongodb

how can I return a specific value for a specific document in MongoDB? For example, I have a schema that looks like:
{
"_id" : "XfCZSje7GjynvMZu7",
"createdAt" : ISODate("2015-03-23T14:52:44.084Z"),
"services" : {
"password" : {
"bcrypt" : "$2a$10$tcb01VbDMVhH03mbRdKYL.79FPj/fFMP62BDpcvpoTfF3LPgjHJoq"
},
"resume" : {
"loginTokens" : [ ]
}
},
"emails" : {
"address" : "abc123#gmu.edu",
"verified" : true
},
"profile" : {
"companyName" : "comp1",
"flagged" : true,
"phoneNum" : "7778883333"
}}
I want to return and store the value for profile.flagged specifically for the document with _id : XfCZSje7GjynvMZu7. So far I have tried:
db.users.find({_id:'myfi3E4YTf9z6tdgS'},{admin:1})
and
db.users.find({_id: 'myfi3E4YTf9z6tdgS'}, {profile:admin});
I want the query to return true or false depending on the assigned value.
Can someone help? Thanks!
MongoDB queries always return document objects, not single values. So one way to do this is with shell code like:
var flagged =
db.users.findOne({_id: 'myfi3E4YTf9z6tdgS'}, {'profile.flagged': 1}).profile.flagged;
Note the use of findOne instead of find so that you're working with just a single doc instead of the cursor that you get with find.
The correct answer here is the method .distinct() (link here)
Use it like this:
db.users.find({_id:'myfi3E4YTf9z6tdgS'},{admin:1}).distinct('admin')
The result will be: 1 or 0

Return latest record from subdocument in Mongodb

Let's say i want to return the latest inserted document from the subdocument. I want to be able to return the second record within the tags array w/ the _id of 54a1845def7572cd0e3fe288
So I far I have this query but it returns all values in the tags array.
db.modules.findOne({_id:"ui","svn_branches.branch":"Rocky"},{"svn_branches.$":1})
Mongodb array:
{
"_id" : "ui",
"svn_branches" : [
{
"updated_at" : ISODate("2013-06-12T20:48:17.297Z"),
"branch" : "Rocky",
"revision" : 0,
"tags" : [
{
"_id" : ObjectId("54a178b8ef7572d30e3fe288"),
"commit_message" : "r277 | ssmith | 2015-02-11 17:43:23 -0400 (Wed, 11 Feb 2015)",
"latest_tag" : "20150218r1_6.32_abc",
"revision" : 1,
"tag_revision_number" : "280",
"updated_at" : ISODate("2015-02-18T19:54:54.062Z")
},
{
"_id" : ObjectId("54a1845def7572cd0e3fe288"),
"commit_message" : "r271 | sam | 2dskjh\n",
"latest_tag" : "20150218r2_6.32_abc",
"revision" : 2,
"tag_revision_number" : "281",
"updated_at" : ISODate("2015-02-19T19:54:54.062Z")
}
]
}
]
}
Simple Solution
Let say we have a category as a document and items as a subdocument.
// find document from collection
const category = await Category.findOne({ _id:'$hec453d235xhHe4Y' });
// fetch last index of sub-document
const lastItemIndex = category.items.length - 1;
// here is the last item of sub-document
console.log(category.items[lastItemIndex]);
as mongodb inserted the latest sub-document at last index, so we need to find the last index for the latest sub-doc.
Queries in MongoDB do not return subdocuments (or, as in your case, subdocuments of subdocuments). They match and return the the documents in the collection. The documents' shape can be changed a bit by projection, but it's limited. If you want to find the latest tag commonly, you probably want to make your documents represent tags. Having an array in an array is generally a bad idea in MongoDB, too.
If this is an uncommon operation, and one that doesn't need to be particularly fast, you can use an aggregation:
db.modules.aggregate([
{ "$unwind" : "$svn_branches" },
{ "$unwind" : "$svn_branches.tags" },
{ "$sort" : { "svn_branches.tags.updated_at" : -1 } },
{ "$group" : { "_id" : "$_id", "latest_tag" : { "$first" : "$svn_branches.tags" } } }
])
I needed to find the last entry of subdocuments and I managed to make it to work with the $slice projection operator: mondodb.com > $slice (projection)
db.modules.find({_id:'ui', 'svn_branches.branch':'Rocky'},
{ 'svn_branches.tags': {$slice:-1} } )
I had only one level, if this doesn't work, please let me know.

mongo db update subdocument - subdocument not in array

I have a document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303
}
},
"_id" : "pl"
}
I am trying to update this document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303,
"yield": 1001, <== inserted a new attribute
}
},
"_id" : "pl"
}
I have tried this,
db.my_collection.update({_id: 'pl'},{$set: {'S.500209.yield': 1}})
But I couldn't make it. And I searched in stack overflow and google but I couldn't find out.
I got so many answers but most of them keeping sub-docuemnts in array.
Pleas help me to solve my issue, and please tell me why most of them keeping subdocuments in array.
Number key might cause the problem. Update your field name.
db.my_collection.update({_id: 'pl'},{$set: {'S.a500209.yield': 1}})
EDIT
Upgrade mongo version. It works fine with 2.4.

Replacing embedded document array with a string array in MongoDB

In my collection, I have a users field as an array of the User collection.
So, it currently looks like this:
{ "name" : "Untitled", "users" : [ { "name" : "Ace Ventura", "datecreated" : "2012-10-05T23:55:56.940Z", "_id" : "740063fb-79c5-4f7f-96e1-907d6ffb1d16" } ], "datecreated" : "2012-10-05T23:55:56.954Z", "_id" : "e207eaea-89f7-48ae-8ba7-b6aa39db2358" }
I'd like to update it so that the array of User collection becomes just an array of the _id property of the User collection. Like so:
{ "name" : "Untitled", "users" : ["740063fb-79c5-4f7f-96e1-907d6ffb1d16" ], "datecreated" : "2012-10-05T23:55:56.954Z", "_id" : "e207eaea-89f7-48ae-8ba7-b6aa39db2358" }
How would I accomplish this?
Thanks in advance.
Alright, I figured it out.
Initially, I was thinking of doing something like this:
db.lists.update(
{},
{
$set: {
users: <oldusers._id>
},
});
However, as it turns out, you cannot reference the current document's properties from within an update().
But, it turns out we can use forEach():
db.lists.find().forEach(function (list) {
// Define length to ensure we have an array of users
var userLength = (list.users && list.users.length) || 0;
var (var i = 0; i < userLength; i++) {
// Ensure the current user object isn't null or undefined
if(list.users[i])
list.users[i] = list.users[i]._id
}
// Finally, save the list object back.
db.lists.save(list)
});
Thank, +gipset, for your answer here.

MongoDB fetch documents with sort by count

I have a document with sub-document which looks something like:
{
"name" : "some name1"
"like" : [
{ "date" : ISODate("2012-11-30T19:00:00Z") },
{ "date" : ISODate("2012-12-02T19:00:00Z") },
{ "date" : ISODate("2012-12-01T19:00:00Z") },
{ "date" : ISODate("2012-12-03T19:00:00Z") }
]
}
Is it possible to fetch documents "most liked" (average value for the last 7 days) and sort by the count?
There are a few different ways to solve this problem. The solution I will focus on uses mongodb's aggregation framework. First, here is an aggregation pipeline that will solve your problem, following it will be an explanation/breakdown of what is happening in the command.
db.testagg.aggregate(
{ $unwind : '$likes' },
{ $group : { _id : '$_id', numlikes : { $sum : 1 }}},
{ $sort : { 'numlikes' : 1}})
This pipeline has 3 main commands:
1) Unwind: this splits up the 'likes' field so that there is 1 'like' element per document
2) Group: this regroups the document using the _id field, incrementing the numLikes field for every document it finds. This will cause numLikes to be filled with a number equal to the number of elements that were in "likes" before
3) Sort: Finally, we sort the return values in ascending order based on numLikes. In a test I ran the output of this command is:
{"result" : [
{
"_id" : 1,
"numlikes" : 1
},
{
"_id" : 2,
"numlikes" : 2
},
{
"_id" : 3,
"numlikes" : 3
},
{
"_id" : 4,
"numlikes" : 4
}....
This is for data inserted via:
for (var i=0; i < 100; i++) {
db.testagg.insert({_id : i})
for (var j=0; j < i; j++) {
db.testagg.update({_id : i}, {'$push' : {'likes' : j}})
}
}
Note that this does not completely answer your question as it avoids the issue of picking the date range, but it should hopefully get you started and moving in the right direction.
Of course, there are other ways to solve this problem. One solution might be to just do all of the sorting and manipulations client-side. This is just one method for getting the information you desire.
EDIT: If you find this somewhat tedious, there is a ticket to add a $size operator to the aggregation framework, I invite you to watch and potentially upvote it to try and speed to addition of this new operator if you are interested.
https://jira.mongodb.org/browse/SERVER-4899
A better solution would be to keep a count field that will record how many likes for this document. While you can use aggregation to do this, the performance will likely be not very good. Having a index on the count field will make read operation fast, and you can use atomic operation to increment the counter when inserting new likes.
You can use this simplify the above aggregation query by the following from mongodb v3.4 onwards:
> db.test.aggregate([
{ $unwind: "$like" },
{ $sortByCount: "$_id" }
]).pretty()
{ "_id" : ObjectId("5864edbfa4d3847e80147698"), "count" : 4 }
Also as #ACE said you can now use $size within a projection instead:
db.test.aggregate([
{ $project: { count: { $size : "$like" } } }
]);
{ "_id" : ObjectId("5864edbfa4d3847e80147698"), "count" : 4 }