I have array of items. inside that i have objects
Please find the structure of one JSON object.
[{ApprovedBy: ""
ApprovedDate: "10/12/2019 7:27:24 AM"
AssignedTo: null
ChildClashes: [{_ClashPoint: {X: "0", Y: "0", Z: "0"},…}, {_ClashPoint: {X: "0", Y: "0", Z: "0"},…},…]
ClashFoundDate: "9/12/2019 7:27:24 AM"
ClashID: "109d3ee3-d470-4534-ac72-0b4f2c3c90eb"
ClashImage: null
ClashName: "New Group"
ClashPublishedBy: "Srini"
ClashStatus: "Reviewed"
Comments: null
Description: "Hard"
Distance: "-2.5"
GroupID: null
ID: 456
ImagePath: null
IsGroup: true
Item1: null
Item2: null
RevisionID: "1"
Viewpoint: ""},
........]
so i want to change the format of approveddate and clashfounddate to dates with MM/dd/YYYY format.
Please let me know how to do it.
please reduce the for loops as much as possible.
thanks in advance.
Inject date pipe in construtor like
constructor(
private datePipe:DatePipe
)
and then
yourdata.forEach(d=>{
d.ClashFoundDate=this.datePipe.transform(d.ClashFoundDate,'MM/dd/yyyy')
d.ApprovedDate=this.datePipe.transform(d.ApprovedDate,'MM/dd/yyyy')
})
date pipe demo
Related
I'm trying to insert data to a collection I created in Atlas MongoDB. The data is following:
[
{ id: 1, performer: 'John Doe', genre: 'Rock', price: 25, day: 1, image: '/img/uploads/1fsd324fsdg.jpg' },
{ id: 2, performer: 'Rebekah Parker', genre: 'R&B', price: 25, day: 1, image: '/img/uploads/2f342s4fsdg.jpg' },
{ id: 3, performer: 'Maybell Haley', genre: 'Pop', price: 40, day: 1, image: '/img/uploads/hdfh42sd213.jpg' }
]
`I get the error : "Insert not permitted while document contains errors."
What am I doing wrong? Please advise.
may be quote problem ...
use double quote to key and property
"id" : 1, "performer : "John Doe" .. ~
This is reslved now, formatting was the problem.
I do the following:
Modify title to "title" and mongo compass works.
Example:
[
{ "id": "1", "performer": "John Doe", "genre": "Rock", "price": "25", "day": "1", "image": "/img/uploads/1fsd324fsdg.jpg" },
]
Reference document: https://docs.mongodb.com/compass/current/documents/insert
It's better that you use MongoDB Compass and connect to it with a connection string:
click on the connection
click on the connection using mongodb compass
then get the compass downloaded from MongoDB according to required OS
but use connection string of connection to MongoDB application
Once you connect to your Compass, you can use import data and then browse your file to use.
I have an array which is result of a query.
r = [
#<Reservation id: 27, schedule_id: 1, name: "test user", gender: "no_answer",
reservation_num: 1, zip: "", prefecture: "", address: "", street: "",
tel: "", note: "", satisfied: true, canceled: false, seq: 27,
created_at: "2019-08-28 06:04:30", updated_at: "2019-08-28 06:04:30",
created_by: 2, updated_by: 2, from_partner: false, no_counting: false,
reservation_time: nil, one_day_payment: nil, payment_id: 123456>
]
I want to get the payment_id but don't understand how should I write.
If you have number of records in array, you can get payment_id in form of array as below,
r.map(&:payment_id)
Looks like you should be able to select the first reservation from that array and then call on the payment_id. Like:
r.first.payment_id
To get all payment_id's you can do
r.map(&:payment_id)
You can also use the collect method
r.collect(&:name)
You can pluck payment ids (from the required set of data) with the following :
Reservation.pluck(:payment_id)
Let's say we have objects like this in a mongodb collection:
{
_id: 00000001
colors: ["green", "yellow"],
houses: [
{
number: 1,
owner: "John"
},
{
number: 2,
owner: "John"
},
{
number:3,
owner: "Dave"
}
]
},
{
_id: 00000002
colors: ["green", "red"],
houses: [
{
number: 15,
owner: "Dave"
},
]
}
So, to get every object where the color array contains the color green the query I would need to write would look smth like this: collection.find({colors: "green"});
Now if I would like to get all the objects in which John owns a house, how would I formulate such a query?
Basically what I am asking is, if my query would be collection.find({houses: {owner: "John", number: ?}}) what would I need to replace the "?" with to tell mongo that I don't care what value number has.
Or maybe there is another approach that I haven't thought of?
Thank you for any help!
(Btw this is a made up example hence why the IDs look weird and the object in itself doesn't seem very useful.)
To query an array of objects you can use the dot notation, try:
db.collection.find({ "houses.owner": "John"}})
I have the following structure:
{
id: "1",
invoices: [{ id: "1", balance: 1},{ id: "2", balance: 1}]
},
{
id: "2",
invoices: [{ id: "3", balance: 1},{ id: "4", balance: 1}]
}
I'm getting a list of invoices IDs that i shouldn't update, the rest i need to update the balance to 0.
I'm pretty new to MongoDB and managing to find a way to do it.
Let say you want to update all invoices of id 1 except invoice.id 2 try this one:
db.collection.update(
{ id: "1", "invoices.id": {$ne: 2} },
{
$set: {
"invoices.$[]": { balance: 0 }
}
}
)
First of all, you forgot the quotes around the field names. Your documents should be like this:
{
"id": "1",
"invoices": [{
"id": "1",
"balance": 1
}, {
"id": "2",
"balance": 1
}]
}
I have limited experience with MongoDB, as I learnt it this semester at University. However, here is my solution:
db.collection.update(
{ id: "1" },
{
$set: {
"invoices.0": { id: "1", balance: 0 }
}
}
)
What does this solution do?
It takes the document with id 1. That is your first document.
The $set operator replaces the value of a field with the specified value. (straight out from the MongoDB manual - MongoDB Manual - $set operator).
"invoices.0" takes the first invoice from the invoices array and then it updates the balance to 100.
Replace the word collection from db.collection with your collection name.
Try and see if it works. If not, I'd like someone with more experience to correct me.
LE: Now it works, try and see.
{
id: "1698627066",
screen_name: "RomanceInfinity",
text: [
"Going NYP to have lunch with bro because I got too much time in between!!!",
"nyp"
],
stance: "",
source: "Twitter for iPhone",
fromid: "411377814521147392",
favourite: "false",
date: "2013-12-13 14:11:28",
replyto: "",
replytoid: "",
retweetfrom: "",
domaintype: "",
keywords: [
"nyp"
],
ratio: "",
latitude: 1000,
longitude: 1000,
retweet: 0,
mood_joy: "0.0",
mood_sadness: "0.0",
mood_surprised: "0.0",
mood_disgusted: "0.0",
mood_anger: "1.0",
_version_: 1454285708574326800
},
{
Is there a way that I count each word in the sentence using facet " Going NYP to have lunch with bro because I got too much time in between!!"?
For example having a results of Going =1 Nyp =1 lunch =1 and also not counting the punctuation?
Extract the text from the dict using:
name_of_dict['text'][0]
This will extract the sentence:
Going NYP to have lunch with bro because I got too much time in between!!!
To ignore any punctuation you can use a function such as .replace() via:
str = "Going NYP to have lunch with bro because I got too much time in between!!!";
print str.replace("!", "");
>>> Going NYP to have lunch with bro because I got too much time in between
Last, I refer you to this post for counting occurance in a string:
Efficiently calculate word frequency in a string
To which a viable solution is:
from collections import Counter
test = 'abc def abc def zzz zzz'
Counter(test.split()).most_common()
[('abc', 2), ('zzz', 2), ('def', 2)]