1659452952
突然変異は JavaScript の世界でかなり頻繁に耳にするものですが、それは正確には何ですか?
この記事では、変数の代入と変更の概念について説明し、それらが一緒になって開発者にとって本当に苦痛になる理由を見ていきます。問題を回避するためにそれらを管理する方法、使用をできるだけ少なくする方法、およびコードを予測可能な状態に保つ方法を見ていきます。
このトピックをさらに詳しく調べたい場合、または最新の JavaScript について理解を深めたい場合は、私の新しい本Learn to Code with JavaScriptの最初の章を無料でチェックしてください。
値型の基本に戻ることから始めましょう…
JavaScript のすべての値は、プリミティブ値またはオブジェクトのいずれかです。7 つの異なるプリミティブ データ型があります。
3
、0
、-4
、などの数字0.625
'Hello'
文字列"World"`Hi`''
true
およびfalse
null
undefined
BigInt
— 大きな整数値を処理するため配列、日付、正規表現、そしてもちろんオブジェクト リテラルを含む、プリミティブ値でないものはすべて object です。関数は特別なタイプのオブジェクトです。これらはプロパティとメソッドを持っているため、間違いなくオブジェクトですが、呼び出すこともできます。
変数の割り当ては、コーディングで最初に学ぶことの 1 つです。たとえば3
、変数に数値を割り当てる方法は次のbears
とおりです。
const bears = 3;
変数の一般的な比喩は、値が内部に配置されたラベルの付いたボックスの 1 つです。上記の例は、「bears」というラベルが含まれ、その中に値 3 が配置されたボックスとして表されます。
何が起こるかについて考える別の方法は、ラベルbears
を の値にマップする参照として3
です。
数値を別の変数に割り当てると、3
bears と同じ値を参照しています。
let musketeers = 3;
変数bears
とmusketeers
両方が同じプリミティブ値 3 を参照します。厳密な等値演算子 を使用してこれを確認できます===
。
bears === musketeers
<< true
true
両方の変数が同じ値を参照している場合、等値演算子は戻ります。
前の例では、プリミティブ値が変数に割り当てられることを示しました。オブジェクトを割り当てるときも同じプロセスが使用されます。
const ghostbusters = { number: 4 };
この割り当ては、変数ghostbusters
がオブジェクトを参照することを意味します。
ただし、オブジェクトを変数に代入する場合の大きな違いは、別のオブジェクト リテラルを別の変数に代入すると、まったく異なるオブジェクトが参照されることです。たとえ両方のオブジェクト リテラルがまったく同じに見えても! たとえば、以下の割り当ては、変数tmnt
(ティーンエイジ ミュータント ニンジャ タートルズ) が変数と同じオブジェクトを参照しているように見えghostbusters
ます。
let tmnt = { number: 4 };
変数ghostbusters
とtmnt
は同じオブジェクトを参照しているように見えますが、厳密な等価演算子で確認するとわかるように、実際には両方ともまったく異なるオブジェクトを参照しています。
ghostbusters === tmnt
<< false
このconst
キーワードが ES6 で導入されたとき、多くの人が JavaScript に定数が導入されたと誤解していましたが、そうではありませんでした。このキーワードの名前は少し誤解を招きます。
で宣言された変数は、const
別の値に再割り当てできません。これは、プリミティブ値とオブジェクトに当てはまります。たとえば、変数bears
は前のセクションで使用して宣言されてconst
いるため、別の値を割り当てることはできません。数値 2 を変数 に代入しようとするとbears
、エラーが発生します。
bears = 2;
<< TypeError: Attempted to assign to readonly property.
数値 3 への参照は固定されており、bears
変数に別の値を再割り当てすることはできません。
同じことがオブジェクトにも当てはまります。変数 に別のオブジェクトを代入しようとするとghostbusters
、同じエラーが発生します。
ghostbusters = {number: 5};
TypeError: Attempted to assign to readonly property.
let
キーワードlet
を使用して変数を宣言すると、後でコード内で別の値を参照するために再割り当てできます。たとえば、 をmusketeers
使用して変数を宣言したlet
ので、参照する値を変更できmusketeers
ます。D'ArtagnanがMusketeersに参加した場合、その数は4に増加します:
musketeers = 4;
let
これは、 が変数の宣言に使用されたために実行できます。参照する値はmusketeers
何度でも変更できます。
変数tmnt
も を使用して宣言されてlet
いるため、別のオブジェクト (または、必要に応じて完全に別の型) を参照するように再割り当てすることもできます。
tmnt = {number: 5};
変数が完全に異なるオブジェクトtmnt
を参照するようになったことに注意してください。プロパティを 5に変更しただけではありません。number
要約すると、 を使用して変数を宣言するとconst
、その値は再割り当てできず、最初に割り当てられたのと同じプリミティブ値またはオブジェクトを常に参照します。を使用して変数を宣言するlet
と、その値はプログラムの後半で必要に応じて何度でも再割り当てできます。
できるだけ頻繁に使用const
することは、変数の値が一定に保たれ、コードの一貫性と予測可能性が向上し、エラーやバグが発生しにくくなるため、一般的には良い方法と見なされます。
ネイティブ JavaScript では、変数にのみ値を割り当てることができます。できるように見えても、別の変数を参照するように変数を割り当てることはできません。たとえば、Stooges の数は Musketeer の数と同じであるため、次を使用してstooges
、変数と同じ値を参照するように変数を割り当てることができます。musketeers
const stooges = musketeers;
以下の図に示すように、これは変数が変数stooges
を参照しているように見えます。musketeers
ただし、これはネイティブ JavaScript では不可能です。変数は実際の値のみを参照できます。別の変数を参照することはできません。このような代入を行うと実際に何が起こるかというと、代入の左側の変数は右側の変数stooges
が参照する値を参照するため、変数は変数と同じ値、musketeers
つまり数値 3を参照します。この割り当てが行われているため、stooges
変数は変数にまったく接続されていませんmusketeers
。
これは、D'Artagnan が Musketeers に参加し、 の値musketeers
を 4 に設定した場合、 の値は 3 のままであることを意味します。実際、 を使用して変数stooges
を宣言したため、新しい値を設定することはできません。常に 3 になります。stoogesconst
要約すると、変数を使用して宣言し、const
それをプリミティブ値に設定すると、別の変数への参照を介しても、その値は変更できません。これは、コードの一貫性と予測可能性が高まることを意味するため、コードに適しています。
値を変更できる場合、その値は変更可能であると言われます。それだけです。ミューテーションとは、値のプロパティを変更する行為です。
JavaScript のすべてのプリミティブ値は不変です。プロパティを変更することはできません。たとえば、文字列"cake"
を variablefood
に割り当てると、そのプロパティを変更できないことがわかります。
const food = "cake";
最初の文字を「f」に変更しようとすると、変更されたように見えます。
food[0] = "f";
<< "f"
しかし、変数の値を見ると、実際には何も変わっていないことがわかります。
food
<< "cake"
length プロパティを変更しようとすると、同じことが起こります。
food.length = 10;
<< 10
length プロパティが変更されたことを示す戻り値にもかかわらず、簡単なチェックでは変更されていないことがわかります。
food.length
<< 4
const
これは、代わりに を使用して変数を宣言することとは関係がないことに注意してくださいlet
。を使用let
した場合、別の文字列を参照するように設定できfood
ますが、そのプロパティを変更することはできません。プリミティブ データ型は不変であるため、そのプロパティを変更することはできません。
逆に、JavaScript のすべてのオブジェクトは変更可能です。つまり、使用して宣言されている場合でも、それらのプロパティを変更できますconst
(覚えておいてlet
、const
変数を再割り当てできるかどうかのみを制御し、可変性とは何の関係もありません)。たとえば、次のコードを使用して、配列の最初の項目を変更できます。
const food = ['🍏','🍌','🥕','🍩'];
food[0] = '🍎';
food
<< ['🍎','🍌','🥕','🍩']
food
を使用して変数を宣言したにもかかわらず、この変更はまだ発生していることに注意してくださいconst
。これは、使用const
してもオブジェクトの変異が停止しないことを示しています。
を使用して宣言されている場合でも、配列の長さプロパティを変更することもできますconst
。
food.length = 2;
<< 2
food
<< ['🍎','🍌']
変数をオブジェクト リテラルに割り当てると、同じように見えても、変数は完全に異なるオブジェクトを参照することに注意してください。
const ghostbusters = {number: 4};
const tmnt = {number: 4};
しかし、変数fantastic4
を別の変数に割り当てると、両方とも同じオブジェクトを参照します。
const fantastic4 = tmnt;
これにより、完全に異なるオブジェクトではなく、変数が参照する同じオブジェクトfantastic4
を参照するように変数が割り当てられます。tmnt
これは、両方の変数が同じオブジェクトを参照するように割り当てられているため、参照によるコピーと呼ばれることがよくあります。
このオブジェクトに加えられた変更は両方の変数で見られるため、これは重要です。
したがって、スパイダーマンがファンタスティック フォーに参加するnumber
場合、オブジェクトの値を更新する可能性があります。
fantastic4.number = 5;
新しいオブジェクトを参照するようにnumber
設定するのではなく、プロパティを変更したため、これは突然変異です。fantastic4
number
のプロパティtmnt
も変更されるため、これは私たちに問題を引き起こします。
tmnt.number
<< 5
これは、 と の両方tmnt
がfantastic4
同じオブジェクトを参照しているためです。そのため、 または のいずれtmnt
かに加えられたfantastic4
変更は両方に影響します。
これは、JavaScript の重要な概念を強調しています。オブジェクトが参照によってコピーされ、その後変更されると、変更はそのオブジェクトを参照する他のすべての変数に影響を与えます。これは、追跡が困難な意図しない副作用やバグにつながる可能性があります。
では、元のオブジェクトへの参照を作成せずにオブジェクトのコピーを作成するにはどうすればよいでしょうか? 答えはスプレッド演算子を使用することです!
スプレッド演算子は、ES2015 では配列と文字列に、ES2018 ではオブジェクトに導入されました。元のオブジェクトへの参照を作成せずに、オブジェクトの浅いコピーを簡単に作成できます。
fantastic4
以下の例は、オブジェクトのコピーを参照するように変数を設定する方法を示していtmnt
ます。tmnt
このコピーはオブジェクトとまったく同じですがfantastic4
、まったく新しいオブジェクトを参照します。これは、オブジェクト リテラル内にコピーする変数の名前を配置し、その前にスプレッド演算子を配置することによって行われます。
const tmnt = {number: 4};
const fantastic4 = {...tmnt};
ここで実際に行ったことは、変数fantastic4
を新しいオブジェクト リテラルに割り当ててから、スプレッド演算子を使用して、tmnt
変数によって参照されるオブジェクトのすべての列挙可能なプロパティをコピーすることです。これらのプロパティは値であるためfantastic4
、参照ではなく値によってオブジェクトにコピーされます。
いずれかのオブジェクトに変更を加えても、もう一方のオブジェクトには影響しません。たとえばnumber
、変数のプロパティfantastic4
を 5 に更新しても、変数には影響しませんtmnt
。
fantastic4.number = 5;
fantastic4.number
<< 5
tmnt.number
<< 4
スプレッド演算子には、オブジェクトのコピーを作成し、1 行のコードで新しいオブジェクトにいくつかの変更を加えるために使用できる便利なショートカット表記法もあります。
たとえば、ティーンエイジ ミュータント ニンジャ タートルズをモデル化するオブジェクトを作成したいとします。最初のタートル オブジェクトを作成し、それに変数leonardo
を割り当てます。
const leonardo = {
animal: 'turtle',
color: 'blue',
shell: true,
ninja: true,
weapon: 'katana'
}
タートルごとに異なるweapon
とプロパティを除いて、他のタートルはすべて同じプロパティを持ちます。次のように、スプレッド演算子を使用して参照するオブジェクトのコピーを作成し、プロパティとプロパティを変更color
することは理にかなっています。leonardoweaponcolor
const michaelangelo = {...leonardo};
michaelangelo.weapon = 'nunchuks';
michaelangelo.color = 'orange';
変更したいプロパティをスプレッド オブジェクトへの参照の後に追加することで、これを 1 行で行うことができます。donatello
変数およびの新しいオブジェクトを作成するコードは次のraphael
とおりです。
const donatello = {...leonardo, weapon: 'bo staff', color: 'purpple'}
const raphael = {...leonardo, weapon: 'sai', color: 'purple'}
この方法でスプレッド演算子を使用すると、オブジェクトの浅いコピーしか作成されないことに注意してください。ディープ コピーを作成するには、これを再帰的に行うか、ライブラリを使用する必要があります。個人的には、オブジェクトをできるだけ浅く保つことをお勧めします。
この記事では、変数の代入と変更の概念について説明し、それらが一緒になって開発者にとって本当に苦痛になる理由を説明しました。
ミューテーションは評判が悪いですが、それ自体が必ずしも悪いわけではありません。実際、動的な Web アプリを構築している場合は、ある時点で変更する必要があります。それがまさに「ダイナミック」という言葉の意味です!これは、コードのどこかにいくつかの変更が必要になることを意味します。そうは言っても、ミューテーションが少ないほど、コードの予測可能性が高くなり、保守が容易になり、バグが発生する可能性が低くなります。
特に有毒な組み合わせは、参照によるコピーと突然変異です。これにより、発生したことに気付いていない副作用やバグが発生する可能性があります。コード内の別の変数によって参照されているオブジェクトを変更すると、追跡が困難な多くの問題が発生する可能性があります。重要なのは、ミューテーションの使用を最小限に抑え、どのオブジェクトがミューテーションされたかを追跡することです。
関数型プログラミングでは、純粋な関数は副作用を引き起こさない関数であり、突然変異は副作用の最大の原因の 1 つです。
ゴールデン ルールは、参照によるオブジェクトのコピーを避けることです。別のオブジェクトをコピーする場合は、spread operator を使用して、コピーを作成した直後にミューテーションを作成します。
ソース: https://www.sitepoint.com/variable-assignment-mutation-javascript/
1622207074
Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.
JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.
JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.
Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.
In JavaScript, ‘document.write‘ is used to represent a string on a browser.
<script type="text/javascript">
document.write("Hello World!");
</script>
<script type="text/javascript">
//single line comment
/* document.write("Hello"); */
</script>
#javascript #javascript code #javascript hello world #what is javascript #who invented javascript
1616670795
It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.
Does your business need an interactive website or app?
Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
Get your business app with JavaScript
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers
1589255577
As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.
#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips
1626321063
PixelCrayons: Our JavaScript web development service offers you a feature-packed & dynamic web application that effectively caters to your business challenges and provide you the best RoI. Our JavaScript web development company works on all major frameworks & libraries like Angular, React, Nodejs, Vue.js, to name a few.
With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.
Looking for professional JavaScript web app development services? We provide custom JavaScript development services applying latest version frameworks and libraries to propel businesses to the next level. Our well-defined and manageable JS development processes are balanced between cost, time and quality along with clear communication.
Our JavaScript development companies offers you strict NDA, 100% money back guarantee and agile/DevOps approach.
#javascript development company #javascript development services #javascript web development #javascript development #javascript web development services #javascript web development company
1670062320
I’m a huge fan of automation when the scenario allows for it. Maybe you need to keep track of guest information when they RSVP to your event, or maybe you need to monitor and react to feeds of data. These are two of many possible scenarios where you probably wouldn’t want to do things manually.
There are quite a few tools that are designed to automate your life. Some of the popular tools include IFTTT, Zapier, and Automate. The idea behind these services is that given a trigger, you can do a series of events.
In this tutorial, we’re going to see how to collect Twitter data with Zapier, store it in MongoDB using a Realm webhook function, and then run aggregations on it using the MongoDB query language (MQL).
There are a few requirements that must be met prior to starting this tutorial:
There is a Zapier free tier, but because we plan to use webhooks, which are premium in Zapier, a paid account is necessary. To consume data from Twitter in Zapier, a Twitter account is necessary, even if we plan to consume data that isn’t related to our account. This data will be stored in MongoDB, so a cluster with properly configured IP access and user permissions is required.
You can get started with MongoDB Atlas by launching a free M0 cluster, no credit card required.
While not necessary to create a database and collection prior to use, we’ll be using a zapier database and a tweets collection throughout the scope of this tutorial.
Since the plan is to store tweets from Twitter within MongoDB and then create queries to make sense of it, we should probably get an understanding of the data prior to trying to work with it.
We’ll be using the “Search Mention” functionality within Zapier for Twitter. Essentially, it allows us to provide a Twitter query and trigger an automation when the data is found. More on that soon.
As a result, we’ll end up with the following raw data:
{
"created_at": "Tue Feb 02 20:31:58 +0000 2021",
"id": "1356701917603238000",
"id_str": "1356701917603237888",
"full_text": "In case anyone is interested in learning about how to work with streaming data using Node.js, I wrote a tutorial about it on the @MongoDB Developer Hub. https://t.co/Dxt80lD8xj #javascript",
"truncated": false,
"display_text_range": [0, 188],
"metadata": {
"iso_language_code": "en",
"result_type": "recent"
},
"source": "<a href='https://about.twitter.com/products/tweetdeck' rel='nofollow'>TweetDeck</a>",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": "227546834",
"id_str": "227546834",
"name": "Nic Raboy",
"screen_name": "nraboy",
"location": "Tracy, CA",
"description": "Advocate of modern web and mobile development technologies. I write tutorials and speak at events to make app development easier to understand. I work @MongoDB.",
"url": "https://t.co/mRqzaKrmvm",
"entities": {
"url": {
"urls": [
{
"url": "https://t.co/mRqzaKrmvm",
"expanded_url": "https://www.thepolyglotdeveloper.com",
"display_url": "thepolyglotdeveloper.com",
"indices": [0, 23]
}
]
},
"description": {
"urls": ""
}
},
"protected": false,
"followers_count": 4599,
"friends_count": 551,
"listed_count": 265,
"created_at": "Fri Dec 17 03:33:03 +0000 2010",
"favourites_count": 4550,
"verified": false
},
"lang": "en",
"url": "https://twitter.com/227546834/status/1356701917603237888",
"text": "In case anyone is interested in learning about how to work with streaming data using Node.js, I wrote a tutorial about it on the @MongoDB Developer Hub. https://t.co/Dxt80lD8xj #javascript"
}
The data we have access to is probably more than we need. However, it really depends on what you’re interested in. For this example, we’ll be storing the following within MongoDB:
{
"created_at": "Tue Feb 02 20:31:58 +0000 2021",
"user": {
"screen_name": "nraboy",
"location": "Tracy, CA",
"followers_count": 4599,
"friends_count": 551
},
"text": "In case anyone is interested in learning about how to work with streaming data using Node.js, I wrote a tutorial about it on the @MongoDB Developer Hub. https://t.co/Dxt80lD8xj #javascript"
}
Without getting too far ahead of ourselves, our analysis will be based off the followers_count
and the location
of the user. We want to be able to make sense of where our users are and give priority to users that meet a certain followers threshold.
Before we start connecting Zapier and MongoDB, we need to develop the middleware that will be responsible for receiving tweet data from Zapier.
Remember, you’ll need to have a properly configured MongoDB Atlas cluster.
We need to create a Realm application. Within the MongoDB Atlas dashboard, click the Realm tab.
For simplicity, we’re going to want to create a new application. Click the Create a New App button and proceed to fill in the information about your application.
From the Realm Dashboard, click the 3rd Party Services tab.
We’re going to want to create an HTTP service. The name doesn’t matter, but it might make sense to name it Twitter based on what we’re planning to do.
Because we plan to work with tweet data, it makes sense to call our webhook function tweet, but the name doesn’t truly matter.
With the exception of the HTTP Method, the defaults are fine for this webhook. We want the method to be POST because we plan to create data with this particular webhook function. Make note of the Webhook URL because it will be used when we connect Zapier.
The next step is to open the Function Editor so we can add some logic behind this function. Add the following JavaScript code:
exports = function (payload, response) {
const tweet = EJSON.parse(payload.body.text());
const collection = context.services.get("mongodb-atlas").db("zapier").collection("tweets");
return collection.insertOne(tweet);
};
In the above code, we are taking the request payload, getting a handle to the tweets collection within the zapier database, and then doing an insert operation to store the data in the payload.
There are a few things to note in the above code:
When we call our function, a new document should be created within MongoDB.
By default, the function will not deploy when saving. After saving, make sure to review and deploy the changes through the notification at the top of the browser window.
So, we know the data we’ll be working with and we have a MongoDB Realm webhook function that is ready for receiving data. Now, we need to bring everything together with Zapier.
For clarity, new Twitter matches will be our trigger in Zapier, and the webhook function will be our event.
Within Zapier, choose to create a new “Zap,” which is an automation. The trigger needs to be a Search Mention in Twitter, which means that when a new Tweet is detected using a search query, our events happen.
For this example, we’re going to use the following Twitter search query:
url:developer.mongodb.com -filter:retweets filter:safe lang:en -from:mongodb -from:realm
The above query says that we are looking for tweets that include a URL to developer.mongodb.com. The URL doesn’t need to match exactly as long as the domain matches. The query also says that we aren’t interested in retweets. We only want original tweets, they have to be in English, and they have to be detected as safe for work.
In addition to the mentioned search criteria, we are also excluding tweets that originate from one of the MongoDB accounts.
In theory, the above search query could be used to see what people are saying about the MongoDB Developer Hub.
With the trigger in place, we need to identify the next stage of the automation pipeline. The next stage is taking the data from the trigger and sending it to our Realm webhook function.
As the event, make sure to choose Webhooks by Zapier and specify a POST request. From here, you’ll be prompted to enter your Realm webhook URL and the method, which should be POST. Realm is expecting the payload to be JSON, so it is important to select JSON within Zapier.
We have the option to choose which data from the previous automation stage to pass to our webhook. Select the fields you’re interested in and save your automation.
The data I chose to send looks like this:
{
"created_at": "Tue Feb 02 20:31:58 +0000 2021",
"username": "nraboy",
"location": "Tracy, CA",
"follower_count": "4599",
"following_count": "551",
"message": "In case anyone is interested in learning about how to work with streaming data using Node.js, I wrote a tutorial about it on the @MongoDB Developer Hub. https://t.co/Dxt80lD8xj #javascript"
}
The fields do not match the original fields brought in by Twitter. It is because I chose to map them to what made sense for me.
When deploying the Zap, anytime a tweet is found that matches our query, it will be saved into our MongoDB cluster.
With tweet data populating in MongoDB, it’s time to start querying it to make sense of it. In this fictional example, we want to know what people are saying about our Developer Hub and how popular these individuals are.
To do this, we’re going to want to make use of an aggregation pipeline within MongoDB.
Take the following, for example:
[
{
"$addFields": {
"follower_count": {
"$toInt": "$follower_count"
},
"following_count": {
"$toInt": "$following_count"
}
}
}, {
"$match": {
"follower_count": {
"$gt": 1000
}
}
}, {
"$group": {
"_id": {
"location": "$location"
},
"location": {
"$sum": 1
}
}
}
]
There are three stages in the above aggregation pipeline.
We want to understand the follower data for the individual who made the tweet, but that data comes into MongoDB as a string rather than an integer. The first stage of the pipeline takes the follower_count
and following_count
fields and converts them from string to integer. In reality, we are using $addFields
to create new fields, but because they have the same name as existing fields, the existing fields are replaced.
The next stage is where we want to identify people with more than 1,000 followers as a person of interest. While people with fewer followers might be saying great things, in this example, we don’t care.
After we’ve filtered out people by their follower count, we do a group based on their location. It might be valuable for us to know where in the world people are talking about MongoDB. We might want to know where our target audience exists.
The aggregation pipeline we chose to use can be executed with any of the MongoDB drivers, through the MongoDB Atlas dashboard, or through the CLI.
You just saw how to use Zapier with MongoDB to automate certain tasks and store the results as documents within the NoSQL database. In this example, we chose to store Twitter data that matched certain criteria, later to be analyzed with an aggregation pipeline. The automations and analysis options that you can do are quite limitless.
If you enjoyed this tutorial and want to get engaged with more content and like-minded developers, check out the MongoDB Community.
This content first appeared on MongoDB.
Original article source at: https://www.thepolyglotdeveloper.com/