Add to all elements from an attributes something - database

I have a collection which contains insides these attributes:
_id, title, year, country. By the way the collection name is movies and it's a Json.
I would like to add before all titles of the document that:
"The title is: NameOfTheTitleHere "
For example I have that:
{"_id": 1,"title": "Gozilla", "year":1998, "country": "USA"}.
And I want that: {"_id": 1,"title": "The title is: Gozilla", "year":1998, "country": "USA"}.
I searched on the doc and the net but I can't find anything. Could someone help me please.
Thanks in avance for anybody who would take the time to help me.

Since MongoDB v4.2, db.collection.update allows conditional updates based on current field values or updating one field using the value of another field(s).
db.movies.update({},
[
{$set:{title:{$concat:["The title is:", " ", "$title"]}}}
],
{multi:true})
Before v4.2, we need to perform MongoDB aggregation and override collection with $out operator:
db.movies.aggregate([
{
$addFields: {
"title" : { $concat: ["The title is:", " ", "$title"] }
}
},
{ $out: "movies" }
])

Related

What is the right prototype of using $out for such requirement?

I am new at MongoDb and I want to save the details of "sellings" that belong to "Tommy" in a new collection called "result1". The collection that I deal with is the following:
"food seller" : {
"seller" : "Tommy",
"sellings" : [
{
"FOOD" : {
"food name" : "Rice"
}
},
{
"FOOD" : {
"food name" : "Noby",
}
}
]
}
I tried the followings, but no doubt, they were going to not work. Not properly!
db.collection.aggregate([ {$project:{"food seller.seller":"Tommy"}}, {$out:"result1"}]);
db.collection.aggregate([ {$project:{"Tommy":"$food seller.seller.sellings"}}, {$out:"result1"} ]);
db.collection.aggregate([ {$project:{"food seller.seller":"Tommy"}}, {$out:"result1"}]);
I know my problem is not getting the right prototype of doing such requirement. Meaning, those codes above are not dealing with the "sellings" array, they just dealing with the "seller".
To sum up, it is wanted to get the "sellings" array of "Tommy" to be in its won collection called "result1".
db.collection.aggregate([{$match:{"food seller.seller":"Tommy"}},{$project:{"food seller.sellings.FOOD":1}},{$out:"result"}]);
This code extracts the "sellings" array contents, that belong to "Tommy", to a new collection called "result". So, if you write db.result.find({}); you will view that array alone. –

MongoDB - how do I pull the record based on the first element in array

Here are 2 documents in the attached file. I need to pull the title field based on the filter (Harrison Ford is the 1st element in the actor field). So I need to pull the 2nd document. Thanks for your help.
{
"_id" : ObjectId("5e66e96a2f86fd04deaa59c5"),
"title" : "Star Trek Into Darkness",
"actors" : [
"Chris Pine",
"Zachary Quinto",
"Harrison Ford",
"Karl Urban"
]
}
{
"_id" : ObjectId("5e66e96a2f86fd04deaa59c6"),
"title" : "Raiders of the lost ark",
"actors" : [
"Harrison Ford",
"Jonathan Frakes",
"Brent Spiner",
"LeVar Burton"
]
}
so you want to get the title of a movie that has Harrison Ford as the first actor right?
if so, give this a try:
db.collection.find(
{ 'actors.0': 'Harrison Ford' },
{ title: 1, _id: 0 }
)
https://mongoplayground.net/p/f4-o13NjYNc
btw, when you say pull it may confuse people because there's a $pull operator in mongodb.
db.collection.update({_id:'Your match id'}, {$pull:{actors:'Jonathan Frakes'}});
you can pull record with $pull and then update that record. Please check below link
https://docs.mongodb.com/manual/reference/operator/update/pull/

How to find documents in MongoDb matching a field and subdocument field that are in an array

The document structure is as follows:
{
"_id" : "V001-99999999",
"vendor_number" : "V001",
"created_time" : ISODate("2016-04-26T22:15:34Z"),
"updated_time" : ISODate("2016-06-07T21:45:46.413Z"),
"items" : [
{
"sku" : "99999999-1",
"status" : "ACTIVE",
"listing_status" : "LIVE",
"inventory" : 10,
"created_time" : ISODate("2016-05-14T22:15:34Z"),
"updated_time" : ISODate("2016-05-14T20:42:21.753Z"),
},
{
"sku" : "99999999-2",
"status" : "INACTIVE",
"listing_status" : "LIVE",
"inventory" : 10,
"created_time" : ISODate("2016-04-26T22:15:34Z"),
"updated_time" : ISODate("2016-06-06T20:42:21.753Z"),
}
]
}
I want to obtain the sku from the item, the conditions are:
1) "vendor_number" = "XXX"
2) items.status = "ACTIVE" AND items.updated_time < [given_date]
Result example:
"sku" : "99999999-2"
or csv:
"sku","99999999-2"
Thank you for your support.
This should be what you want. Although I'm assuming you wanted "status": "active"?
db.getCollection('collection').aggregate([
{ $match: { "vendor_number": "XXXX" } },
{ $project: {
"items": {
$filter: {
input: "$items",
as: "item",
cond: { $eq: ["$$item.status", "ACTIVE"] } // or maybe ["$$item.listing_status", "LIVE"] ?
}
}
}
},
{ $project: { "items.sku": true } }
])
I love using aggregation to manipulate stuff. It's great all the things you can do with it. So here's what's going on:
The first part is simple. The $match step in the aggregation pipeline says just give me documents where vendor_number is "XXXX".
The next part is a bit hairy. The first projection step creates a new field, called "items", I could have called it "results" or "bob" if I wanted to. The $filter specifies which items should go into this new field. The new "items" field will be an array that will have all the results from the previous items field, hence the input: "$items", where you're using the keyword "item" to represent each input item that comes into the filter. Next, the condition says, for each item, only put it in my new "items" array if the item's status is "ACTIVE". You can change it to ["$$items.listing_status", "LIVE"] if that's what you needed. All of this will pretty much give you you're result.
The last project just get's rid of all other fields except for items.sku in each element in the new "items" array.
Hope this help. Play around with it and see what else you can do with the collection and aggregation. Let me know if you need any more clarification. If you haven't used aggregation before, take a look at the aggregation docs and the list of pipeline operators you can use with aggregation. Pretty handy tool.

Meteor mongo updating nested array

Example document:
{
"_id" : "5fTTdZhhLkFXpKvPY",
"name" : "example",
"usersActivities" : [
{
"userId" : "kHaM8hL3E3As7zkc5",
"startDate" : ISODate("2015-06-01T00:00:00.000Z"),
"endDate" : ISODate("2015-06-01T00:00:00.000Z")
}
]
}
I'm new in mongoDB and I read other questions about updating nested array and I can't do it properly. What I want to do is to change startDate and endDate for user with given userId. My problem is that it always pushes new object to array instead of changing object with given userId.
Activity.update(
_id: activityId, usersActivities: {
$elemMatch: {
userId: Meteor.userId()
}
}},
{
$push: {
'usersActivities.$.startDate': start,
'usersActivities.$.endDate': end
}
}
);
I will be really glad of help.
So the first thing to say here is the $elemMatch is not required in your case as you only want to match on a single array property. You use that operator when you need "two or more" properties from the same array element to match your conditions. Otherwise you just use "dot notation" as a standard.
The second case here is with $push, where that particular operator means to "add" elements to the array. In your case you just want to "update" so the correct operator here is $set:
Activity.update(
{ "_id": activityId, "usersActivities.userId": Meteor.userId() },
{
"$set": {
'usersActivities.$.startDate': start,
'usersActivities.$.endDate': end
}
}
)
So the positional $ operator here is what matches the "found index" from the array element and allows the $set operator to "change" the elements matched at that "position".
"What if Meteor.userId() does not exist, how to insert the whole of object with userID, startDate and endDate? – justdiehard Jun 14 at 20:20"
If you try to put new, you should take a look at Meteor Accounts package, there are methods like
Accounts.createUser(YOU_USER_SCHEME)

Adding a view that displays user data using store Sencha touch2

My store JSON looks like this:
"content" : {
"RecId" : "123",
"CustNo" : "123",
"Attn" : "",
"AcctManagFirst": "Sample ",
"AcctManagLast": "Sample2",
"Name": "Works",
"Terms": "1234",
"Freight":0,
"Taxable":1,
"TaxTable": "EXEMPT",
"TaxTableRate": "0.000000",
"BillToAddr": "GGGGGGGGG",
"BCity": "New Iberia",
"BState": "LA",
"BZip": "70560",
"WHAddr": "Perry",
"WHCity": "New Iberia",
"WHState": "LA",
"WHZip": "70560"
}
Using the above I want create a view that displays user information like:
Name {Name from the store}
AccountManager Last NAme {AcctManagLast from the store}
I am new to Sencha . Plese help me how to write form that displays the label followed by value from store.
A simple panel with template and data will work for you, check out : http://www.senchafiddle.com/#KmNX8#yqVQt

Resources