I'm trying to re-map twitter direct messages data into conversation for mongodb.
"events": [
{
"type": "message_create",
"id": "1023372540847312900",
"created_timestamp": "1532826001737",
"message_create": {
"target": {
"recipient_id": "605675237"
},
"sender_id": "1020464064684806146",
"source_app_id": "268278",
"message_data": {
"text": "all right mate thank you too",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023372444210524164",
"created_timestamp": "1532825978697",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "605675237",
"message_data": {
"text": "Okay thank you mate, this is distinquish",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023372325150965765",
"created_timestamp": "1532825950311",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "69565292",
"message_data": {
"text": "Hello strow bree how are you",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023245595790778372",
"created_timestamp": "1532795735677",
"message_create": {
"target": {
"recipient_id": "605675237"
},
"sender_id": "1020464064684806146",
"source_app_id": "268278",
"message_data": {
"text": "Once at a social gathering, Gladstone said to Disraeli",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023245133637140484",
"created_timestamp": "1532795625491",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "605675237",
"message_data": {
"text": "On Krat's main screen appeared the holo image of a man, and several dolphins. From the man's shape, Krat could tell it was a female, probably their leader. \"...stupid creatures unworthy of the name `sophonts.' Foolish, pre-sentient upspring of errant masters. We slip away from all your armed might, laughing at your clumsiness! We slip away as we always will, you pathetic creatures. And now that we have a real head start",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
}
],
"apps": {
"268278": {
"id": "268278",
"name": "Twitter Web Client",
"url": "http://twitter.com"
}
}
}
I'm trying to change this data into this;
[{
members: [ '605675237', '1020464064684806146' ],
messages: [
{ author: '605675237', body: 'On Krats main screen appeared the holo image of a man, and several dolphins. From the mans shape, Krat could tell it was a female, probably their leader ...stupid creatures unworthy of the name sophonts Foolish, pre-sentient upspring of errant masters. We slip away from all your armed might, laughing at your clumsiness! We slip away as we always will, you pathetic creatures. And now that we have a real head start' },
{ author: '1020464064684806146', body: 'Once at a social gathering, Gladstone said to Disraeli' }
{ author: '605675237', body: 'Okay thank you mate, this is distinquish' }
{ author: '1020464064684806146', body: 'all right mate thank you too' },
]
},
{
members: ['69565292', '1020464064684806146'],
messages: [
{ author: '69565292', body: 'Hello strow bree how are you' }
]
}]
user_id1 must be sender_id and user_id2 must be recipient_id but actually, I have to group the twitter DM Objects by sender_id and recipient_id probably.
How can I solve this problem easily any suggestion?
Maybe we can use Lodash or Underscore to solve this remapping easily.
The solution is the group the messages by the user ids, I have created a hashed from the ids to fit it into an object:
const normalized = {};
data.events.forEach(event => {
// Just of easier access
const message = event.message_create;
// Create a temp hash which identify 2 user conversation.
// The hash is just the 2 ids with a `|` separated them
const hashedUsers = [message.sender_id, message.target.recipient_id].sort().join('|');
// The end object you wanted
const normalizedMessage = {
author: message.sender_id,
body: message.message_data.text
};
// Check if we already have this 2 users conversations, create a list of their messages
if (!normalized.hasOwnProperty(hashedUsers)) {
normalized[hashedUsers] = [normalizedMessage];
} else {
normalized[hashedUsers].push(normalizedMessage);
}
});
// Now we have a normalized object, the keys are the hash we created and the values are their messages
// so we can create the object you wanted:
const output = []
Object.entries(normalized).forEach(([key, value]) => {
output.push({
members: key.split('|'),
messages: value
})
})
Related
I have the following structure of Student collection in MongoDb:
{
"_id": "st1",
"student_courses": [
{
"_id": "c1",
"course_name": "Node",
"image": [
{
"_id": "c1img1",
"image": "img1.jpeg"
}
]
},{
"_id": "c2",
"course_name": "React",
"image": [
{
"_id": "c2img2",
"image": "img2.jpeg"
}
]
}
],
}
Now, I want to update the image name of img1.jpeg in all the documents that have same image name. So what I am doing is this:
Student.updateMany(
{ "student_courses._id": "c1"},
{
$set: {
"student_courses.$.course_name": "Node Crash Course",
"student_courses.$.image[0].image": complete_image_name,
},
}
);
Unexpectedly, this is updating course_name field but image. I have tried using $ positional argument instead of [0] but got the error Too many positional arguments ...... I don't know how to do that. My expected output should look like this:
{
"_id": "st1",
"student_courses": [
{
"_id": "c1",
"course_name": "Node Crash Course",
"image": [
{
"_id": "c1img1",
"image": "complete_image_name.jpeg"
}
]
},
:::::::::::::::::::
:::::::::::::::::::
],
}
{
"_id": "st2",
"student_courses": [
{
"_id": "c1",
"course_name": "Node Crash Course",
"image": [
{
"_id": "c1img1",
"image": "complete_image_name.jpeg"
}
]
},
:::::::::::::::::::
:::::::::::::::::::
],
}
Moreover, I have implemented almost every method posted in similar questions. Thanks in advance for any help.
Try to change the way you are referring to the first element in the array:
Student.updateMany(
{ "student_courses._id": "c1"},
{
$set: {
"student_courses.$.course_name": "Node Crash Course",
"student_courses.$.image.0.image": complete_image_name,
},
}
);
This is my posting document in MongoDB:
{
"_id": {
"$oid": "5b4e60ab24210138f5746402"
},
"type": [
"full",
"temp"
],
"applications": [
{
"_id": {
"$oid": "5b52113d1123631744fa9f39"
},
"applicationDate": {
"date": 20,
"day": 5,
"hours": 18,
"minutes": 43,
"month": 6,
"seconds": 41,
"time": 1532105021753,
"timezoneOffset": -120,
"year": 2018
},
"userId": {
"$oid": "5b51fb6f9686430cee31a0d9"
},
"resume": {
"fieldname": "resume",
"originalname": "resume_acc.pdf",
"encoding": "7bit",
"mimetype": "application/pdf",
"destination": "./public/resumes/",
"filename": "765d650b9014cc3ddadb801d10d495a5",
"path": "public/resumes/765d650b9014cc3ddadb801d10d495a5",
"size": 8
},
"coverLetter": {
"fieldname": "docs",
"originalname": "cover letter.pdf",
"encoding": "7bit",
"mimetype": "application/pdf",
"destination": "./public/resumes/",
"filename": "e5892869b24f3fc5e72d3e057b4dd61d",
"path": "public/resumes/e5892869b24f3fc5e72d3e057b4dd61d",
"size": 5
}
}
],
"creatorId": {
"$oid": "5b4b95cc16778c325010a55d"
},
"title": "Developer",
"salary": "50/h",
"timeLine": "One year",
"description": "You'll be building apps",
"duties": "Building apps",
"experience": "2 years",
"province": "ON",
"visible": true,
"__v": 0
}
Postings is an array of posting, which look like the above document. applications is an array which is in every posting. I want to search all postings.applications to see get all postings the user applied to. For now I tried to do it like this:
var Posting = require('../models/posting');
var postings = await Posting
.find({'visible': true});
console.log('posts', postings);
var applications = await Posting
.find({'visible': true})
.where(postings
.map(posting => posting.applications
.map(application => application.userId.equals(req.user._id)))
);
But obviously this failed.
I tried this as well:
var postings = await Posting
.find({'visible': true, 'applications[$].userId': req.user._id});
or
var postings = await Posting
.find({'visible': true, 'applications.$.userId': req.user._id});
But no luck. They both return an empty array.
Posting model:
var mongoose = require('mongoose');
jobPostingSchema = mongoose.Schema({
"creatorId": mongoose.Schema.Types.ObjectId, //ObjectID('aaaa'), // ID of the User or Account
"eventId": {'type': mongoose.Schema.Types.ObjectId, 'default': undefined},
"title": String,
"type": [], //"Full", // What this means? I did not understand.
"salary": String,
"timeLine": String, // What this means? I did not understand.
"description": String,
"duties": String,
"experience": String,
"province": String, // Employer will post job postings based on the province and region
// Applications means, how many people applied for this job post?
"applications": [
// {
// ObjectID: cccc,
// userId: dddd,
// Resume: {},
// coverLetter: String,
// },
],
"visible": Boolean,
});
module.exports = mongoose.model('posting', jobPostingSchema);
So how can I get all applications where userId equals req.user._id?
Maybe this works as a solution ( sourcing from the SO link shared by #DSCH here ):
Posting.find({
'applications': {
$elemMatch: { userId: req.user._id }
},
'visible:': true
});
If you wish to seek clarification on how it works, you may refer to the link here
Posting.find({
'visibile:': true,
'applications': {
$elemMatch: { userId: req.user._id }
}
});
$elemMatch is the mongo operator that you probably need.
Hope that one helps better.
I am using actions-on-goolge library for nodejs https://www.npmjs.com/package/actions-on-google
How would I able to get the whole JSON response, or use id string inside my intent function? I have tried to print out the input, it only gives the query part of the JSON. Tried to look up their documentation, it does not seem to explain how I could get back the whole JSON file.
https://developers.google.com/actions/reference/nodejs/lib-v1-migration
I am beginner of javascript.
The JSON request from simulator:
{
"user": {
"userId": "ABwppHEAPgcgb2yFUFURYFEJGg4VdAVcL9UKO9cS7a7rVfasdasdt67LzgrmMseTvb5mmJjbjj7UV",
"locale": "en-US",
"lastSeen": "2018-05-11T23:14:42Z",
"userStorage": "{\"data\":{}}"
},
"conversation": {
"conversationId": "1526080586367",
"type": "NEW"
},
"inputs": [
{
"intent": "com.example.device.OFF",
"rawInputs": [
{
"inputType": "KEYBOARD",
"query": "Talk to MyDevice to turn off"
}
],
"arguments": [
{
"name": "trigger_query",
"rawText": "turn off",
"textValue": "turn off"
}
]
}
],
"surface": {
"capabilities": [
{
"name": "actions.capability.MEDIA_RESPONSE_AUDIO"
},
{
"name": "actions.capability.WEB_BROWSER"
},
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
}
]
},
"isInSandbox": true,
"availableSurfaces": [
{
"capabilities": [
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
}
]
}
]
}
My Node JS script base on the example:
const express = require('express')
const bodyParser = require('body-parser')
const {
actionssdk,
Image,
} = require('actions-on-google')
app.intent('actions.intent.TEXT', (conv, input) => {
if (input === 'bye' || input === 'goodbye') {
return conv.close('See you later!')
}
conv.ask(`I didn't understand. Can you tell me something else?`)
})
app.intent('com.example.MyDevice.TEST', (conv, input) => {
console.log(input);
console.log(conv.action);
console.log(conv.intent);
conv.close('Test Done');
});
express().use(bodyParser.json(), app).listen(3000)
You should be able to get the entire JSON request through conv.body.
I am a newbie to NodeJS and Sails.js.
I want create a REST API that allows me to expand a resource based on query parameter. For eg
HTTP GET /snippets
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "htep://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": "http://localhost:8000/users/2/",
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
HTTP GET /snippets?expand=owner
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "http://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": {
"url": "http://localhost:8000/users/2/",
"username": "test",
"email": "test#test.com"
},
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
Wondering how can I do that in Sails.js or NodeJS?
You should use assocations.
Here is how you would create a one-to-many association between your User model and your Snippet model:
// User.js
module.exports = {
// ...
attributes: {
// ...
snippets: {
collection: 'Snippet',
via: 'owner'
}
}
};
// Snippet.js
module.exports = {
// ...
attributes: {
// ...
owner: {
model: 'User'
}
}
};
Then you can hit /snippets if you want a list of snippets, and hit /snippets?populate=[owner] if you need details about the owners of the snippets.
I am quite new to Loopback and NodeJS, so please tell me if there is a "Node way" of doing something that I am missing. I decided to write a basic application to try and learn more.
I have two models, 'UserInformation' and 'ClothingArticle'. I have created a 'hasMany' relation from UserInformation to ClothingArticle.
As a basic test, I wanted to add a remote method to UserInformation to get recommendations for ClothingArticles. However, I cannot seem to get access to anything related to ClothingArticles. I added code into the common/models/user-information.js file to try and retrieve information about the relation, but am not sure if this is even the right spot to be putting it.
My code is below, could you help?
common/models/user-information.js:
module.exports = function(UserInformation) {
get_methods = function(obj) {
var result = [];
for(var id in obj) {
try {
if(typeof(obj[id]) == "function") {
result.push(id + " (function): "); //+ obj[id].toString());
}
else
result.push(id + ": "); // + obj[id].toString());
}
catch (err) {
result.push(id + ": inaccessible");
}
}
return result;
}
// This doesn't anything about my new relations?
console.log(get_methods(UserInformation.prototype));
UserInformation.recommendations = function(source, callback) {
var response = "I don't have any recommendations.";
var test_function = UserInformation.findById(3, function(err, instances) {
if(err) return console.log("Errors: " + err);
console.log("Instances: " + String(instances));
// Nothing here either about the relations.
console.log(get_methods(UserInformation));
console.log(UserInformation.app);
/*
instances.clothingArticles.create({
id:92,
colors:['red','blue']
});
*/
console.log("Created a new clothing article.");
});
console.log (response);
callback(null, response);
}
UserInformation.remoteMethod(
'recommendations',
{
accepts: [
{arg: 'source', type: 'string'} // Used to mark the source (closet, generic, etc)
],
http: {path: '/recommendations', verb: 'get'},
returns: {arg: 'recommendations', type: 'string'}
}
);
};
common/models/user-information.json:
{
"name": "UserInformation",
"base": "PersistedModel",
"strict": false,
"idInjection": false,
"properties": {
"birthday": {
"type": "date"
},
"id": {
"type": "number",
"id": true,
"required": true
},
"eye_color": {
"type": "string"
},
"hair_color": {
"type": "string"
},
"weight": {
"type": "string",
"comments": "pounds"
},
"height": {
"type": "number",
"comments": "inches"
}
},
"validations": [],
"relations": {
"clothingArticles": {
"type": "hasMany",
"model": "ClothingArticle",
"foreignKey": "owner_id"
}
},
"acls": [],
"methods": []
}
common/models/clothing-article.json:
{
"name": "ClothingArticle",
"base": "PersistedModel",
"strict": false,
"idInjection": false,
"properties": {
"id": {
"type": "number",
"id": true,
"required": true
},
"colors": {
"type": [
"Color"
],
"required": true
},
"owner_id": {
"type": "number"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}
I suggest starting with our getting started example and working your way through through the tutorial series here: https://github.com/strongloop/loopback-example
The questions you ask are answered throughout the examples (ie. model relations). To answer your question, if you defined a relation properly, you should be able to access the relation via dot.
...
UserInformation.ClothingArticle...
...
See http://docs.strongloop.com/display/LB/HasMany+relations for more information.