How to count matched elements from an Array in another Array MongoDB

arrays
Question: I am using the MongoDB Aggregate framework to query a users collection. This is the is an example of the user schema layout below:{ "_id" : ObjectId("XXXXXXXXXXXXXX367db"), "updatedAt" : ISODate("2017-08-18T10:59:54.904Z"), "createdAt" : ISODate("2017-08-18T10:59:54.904Z"), "email" : "fake57@gmail.com", "firstName" : "Tianna.", "gender" : "male", "geometry" : { "coordinates" : [ -6.26119, 53.35247 ], "_id" : ObjectId("5996c8a9a4d84d3639c367dc"), "type" : "point" }, "age" : 25, "personalAttributes" : [ "ksjdnksajdna", "ksjdssacasca", "xz12nksajdna", "xz12nksaxxna", "xz12nksaxxxx", "xz12nwwzwwwa", "xz12nkslkmna", ] } This is steps outlined in the aggregate pipeline. 1: Geolocate users within a specified distance using the $geoNear operator. 2: Match the users based on gender. db.getCollection('users').aggregate([ { "$geoNear": { "near": { "type": "Point", "coordinates": [ -6.26030969999999, 53.3498053 ] }, "distanceField": "dist.calculated", "spherical": true, "maxDistance": 770000 } }, { "$match": { "gender": "male" } } ])…
Read More

How to set multiple request headers on each request

Uncategorized
Question: I'm not sure how to set multiple request headers for each ajax request. This is what I have now:Ext.Ajax.on("beforerequest", function(conn, request){ request.headers = request.headers || {}; return request.headers.Authorization = "Bearer " + Ext.util.Cookies.get("token"); }, this); As you can see, I'm setting here just one Authorization token. But now I want to set one more header in order to submit so called "refresh" token to the server. How can I do that? By the way, I need to access cookies and set headers on each request, because data in this cookies may change. So I can not set headers once and for all time. Answer: Just add multiple headers to the headers object. Also note you don't need (or want) to return the value of any of those headers from…
Read More

Django form -Fill in duration field when 2 DateField were selected

Uncategorized
Question: I have a form for employee to apply leaves, how can I make by selecting the start date and end date field in the form it will calculate the duration in days. How can I do the calculation in view.py? Any help is much appreciated Below is my code : model.py :class Leave(models.Model):employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='+') start_date = models.DateField() end_date = models.DateField() duration = models.FloatField() form.py : class LeavesDetailForm(forms.ModelForm):class Meta:model = Leave fields = ('employee','start_date', 'end_date', 'duration',) widgets = {'start_date': forms.DateInput(attrs={'type': 'date'}), 'end_date': forms.DateInput(attrs={'type': 'date'}), 'employee': forms.HiddenInput()} view.py :def my_leaves_view(request):form = LeavesDetailForm(request.POST or None) leaves_log = Leave.objects.all().filter(employee=request.user.profile.employee.id) for annual_field in leaves_log:annual_duration = annual_field.end_date - annual_field.start_date print(annual_duration) if form.is_valid():inst = form.save(commit=False) inst.save() return HttpResponseRedirect('my_leaves_content.html') context = {'form': form} return render(request, 'hrm/my_leaves/my_leaves_content.html', context) Answer: A subtraction of two dates gives…
Read More

Adding offset and delay

Uncategorized
Question: I have a signal into which I want to introduce several offsets and delays, where offsets range from 0.5 to 5 and delays range from 1 to 7. I'm providing an example signal here to demonstrate the problem I'm having, but the size of my real data is 1x1666520. How do I introduce these changes to the signal? Example code:t = [ 0 : 1 : 50]; % Time Samples f = 45; % Input Signal Frequency Fs = 440; % Sampling Frequency data = sin(2*pi*f/Fs*t)'; T.InputOffset = 5; T.OutputOffset = 5; addoffset = retrend(data); Y = step(delay,data); figure(); plot(t,addoffset,t,Y); Answer: When trying to run your example code, I'm getting this error: Undefined function retrend for input arguments of type double. The cause of this is that the retrend…
Read More

Squid not entirely running as nonroot user

Uncategorized
Question: [root@xxxxxx ~]# ps -ef | grep squid root 26800 1 0 04:13 ? 00:00:00 squid -f /etc/squid/squid.conf squid 26802 26800 0 04:13 ? 00:00:00 (squid) -f /etc/squid/squid.conf squid 26804 26802 0 04:13 ? 00:00:00 (unlinkd)" One of the process starts up with root. The changes that was required was made on squid.conf cache_effective_group squid cache_effective_user squid added all ownership for the user and group. Answer: Read more
Read More

Fragment should be static such that they can be re-instantiated by the system

Uncategorized
Question: I am using Payumoney payment gateway in my app, everything was good until i decided to generate signed apk. It shows this error: Error:Error: Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static [ValidFragment]. It is generating normal apk though. Here is my code:public class OldPaymentActivity extends AppCompatActivity implements MagicRetryFragment.ActivityCallback{ Bundle bundle; String url; boolean cancelTransaction = false; PayuConfig payuConfig; private BroadcastReceiver mReceiver = null; private String UTF = "UTF-8"; private boolean viewPortWide = false; private WebView mWebView; private Boolean smsPermission; private int storeOneClickHash; private String merchantHash; MagicRetryFragment magicRetryFragment; String txnId = null; public OldPaymentActivity(){ } @Override protected void onCreate(Bundle savedInstanceState) { /** * when the device runing out of memory we dont want the user to restart the…
Read More

PHP: Including files dependent on each other

Uncategorized
Question: Two files are dependent on each other: FILE1:$var1 = 'Straw ' . $var2; FILE2:$var2 = ' berry'; $var3 = $var1; FILE3:// this file should include FILE1 and FILE2 How should one go about including FILE1 and FILE2 in FILE3 in a way that makes sure the three variables are properly populated? Thank you. Answer: You actually will get a warning about an undefined variable regardless of the order you include them due to your design — since file 1 requires $var2 and file 2 requires $var1. Instead do something like this:$var2 = 'berry'; $var1 = 'Straw ' . $var2; $var3 = $var1; You can split these up in to separate files so long as they are called in the above order. Read more
Read More

Concatenate many rows into a single text string?

Uncategorized
Question: Consider a database table holding names, with three rows:Peter Paul Mary Is there an easy way to turn this into a single string of Peter, Paul, Mary? Answer: Use COALESCE:DECLARE @Names VARCHAR(8000) SELECT @Names = COALESCE(@Names + ', ', '') + Name FROM People Just some explanation (since this answer seems to get relatively regular views):Coalesce is really just a helpful cheat that accomplishes two things: 1) No need to initialize @Names with an empty string value. 2) No need to strip off an extra separator at the end. The solution above will give incorrect results if a row has a NULL Name value (if there is a NULL, the NULL will make @Names NULL after that row, and the next row will start over as an empty string…
Read More

How to distribute swift build in linux

Uncategorized
Question: I made a swift program in Linux. I followed the tutorial mentioned at Swift Official Site. I am unable to find a way to share my build as a software. The site guides how to generate a build. It is done by following commands swift package init --type executable Once I have written the code in main.swift file. I built release build using command swift build --configuration release Now the build is at .build/release/MySwiftBuild Please guide me a way to make it available to other C or C++ programs in the linux machine. I don't want to always go in the project folder and run the program manually. The program should be able to be called by other programs from anywhere in the system. Answer: Read more
Read More

Dynamically disabling touch-action (overscroll) for SVG elements

Uncategorized
Question: I'm having problems with touch screen overscrolling on Chrome. I have a document with am SVG element in it, which contains some shape, say a rectangle: Now, I want to make the rectangle draggable, which means that I want to disable all kinds of touch actions on the respective element, by setting it's style property touch-action: none. This works fine on all desktop browsers, except Chrome. On Chrome, when I touch and move on a rectangle, browser's overscroll feature kicks in. This causes the browser window to awkwardly move, as well as all Pointer events that I have set on the rectangle cancelled. I.e. pointermove is registered for a fraction of second, then it just stops when overscroll kicks in. pointerup is never called even when touch is released.…
Read More