how to use count and or expression at same time golang mongodb? - mongodb

hi i write this line of codes but when i try it just return when total is greater then number i gave and it just doesn't use checked . i mean i have 6 documants and 5 of them are true but it give me 6 reuslt !
checked := bson.D{{"$or", []interface{}{bson.D{{"checked", false}}, bson.D{{"checked", nil}}}}}
totalReport := bson.D{{"total", bson.D{{"$gte", config.ReportNumberToChangeNickname}}}}
totalReportAndChecked := bson.D{{"$and", []interface{}{checked, totalReport}}}
matchStage := bson.D{{"$match", totalReportAndChecked}}
groupStage := bson.D{{"$group", bson.D{{"_id", "$user_id"}, {"total", bson.D{{"$sum", 1}}}}}}
cursor, err := UserReportDb.Aggregate(ctx, mongo.Pipeline{groupStage, matchStage})
if err != nil {
fmt.Println(err)
return &mongo.Cursor{}, err
}

i found the answer thanks
cond := bson.D{{"$cond", []interface{}{"$checked", 0, 1}}}
matchStage := bson.D{{"$match", bson.D{{"$expr", bson.D{{"$gte", []interface{}{"$total", config.ReportNumberToChangeNickname}}}}}}}
groupStage := bson.D{{"$group", bson.D{{"_id", "$user_id"}, {"total", bson.D{{"$sum", cond}}}, {"value", bson.D{{"$last", "$value"}}}}}}
cursor, err := UserReportDb.Aggregate(ctx, mongo.Pipeline{groupStage, matchStage})

Related

Can't obtain solution for the variable gamma1, what's wrong?

I want to use the following code
f := sqrt(7200*8200)*10^6:
Ig := 0, 9330; Ug := 13.134:
f_gr := 10^10:
r_e := .8:
L_e := .5*10^(-9):
C_e := 2.5*10^(-12):
Cka := .4*10^(-12):
Ckp := .8*10^(-12):
L_b := .3*10^(-9):
I_0 := I*f*Ig*(cos(.4*f/f_gr)+I*sin(.4*f/f_gr))/(f_gr*gamma1):
I_e := I_0+Ig:
U_e := I_e*(r_e+I*(2*Pi*f*L_e)):
U_p := -I*(1-gamma1)*I_0/(2*Pi*f*C_e):
Uska := Ug+U_p:
Iska := I*(2*Pi*f*Cka*Uska):
Irb := I_0+Iska:
Urb := 2.8*Irb:
Uskp := Urb+Uska:
Iskp := I*(2*Pi*f*Ckp*Uskp):
Irk := Uskp/773.5:
Ib := Irb+Iskp+Irk:
Ulb := I*(2*Pi*f*L_b*Ib):
Uv := U_e+Ulb+Urb+U_p:
Ik := Ig-Iska-Iskp-Irk:
Uk := Ug-U_e:
to solve Re(Uk)*Re(Ik)+Im(Uk)*Im(Ik)=0.186 for gamma1 (I is for imaginary unit). But this doesn't work. I've tried to use brute force, assigning some numbers to gamma1 and then finding Re(Uk)*Re(Ik)+Im(Uk)*Im(Ik), but I can't obtain a number anyway. Could you help me?
Your code has an apparent mistake in the definition of Ig, to which you have assigned the expression sequence 0, 9330.
Perhaps you intended Ig:=0.9330 instead.
restart;
f := sqrt(7200*8200)*10^6:
Ig := 0.9330:
Ug := 13.134:
f_gr := 10^10:
r_e := .8:
L_e := .5*10^(-9):
C_e := 2.5*10^(-12):
Cka := .4*10^(-12):
Ckp := .8*10^(-12):
L_b := .3*10^(-9):
I_0 := I*f*Ig*(cos(.4*f/f_gr)+I*sin(.4*f/f_gr))/(f_gr*gamma1):
I_e := I_0+Ig:
U_e := I_e*(r_e+I*(2*Pi*f*L_e)):
U_p := -I*(1-gamma1)*I_0/(2*Pi*f*C_e):
Uska := Ug+U_p:
Iska := I*(2*Pi*f*Cka*Uska):
Irb := I_0+Iska:
Urb := 2.8*Irb:
Uskp := Urb+Uska:
Iskp := I*(2*Pi*f*Ckp*Uskp):
Irk := Uskp/773.5:
Ib := Irb+Iskp+Irk:
Ulb := I*(2*Pi*f*L_b*Ib):
Uv := U_e+Ulb+Urb+U_p:
Ik := Ig-Iska-Iskp-Irk:
Uk := Ug-U_e:
And now,
solve(Re(Uk)*Re(Ik)+Im(Uk)*Im(Ik)=0.186, {gamma1});
{gamma1 = -0.9296926790 - 0.6316639400 I}

Best way to index large file

I have a file with about 100gb with a word:tag per line. I want to index these on word to easily get the list of tags for a given word.
I wanted to save this on boltdb (mainly to checkout boltdb) but random write access is bad so I was aiming to index the file in some other way first, then moving all of it to boltdb without need to check for duplicates or de/serialisation of the tag list
So, for reference, if I simply read the file into memory (discarding data), I get about 8 MB/s.
If I write to boltdb files using code such as
line := ""
linesRead := 0
for scanner.Scan() {
line = scanner.Text()
linesRead += 1
data := strings.Split(line, ":")
err = bucket.Put([]byte(data[0]), []byte(data[1]))
logger.FatalErr(err)
// commit on every N lines
if linesRead % 10000 == 0 {
err = tx.Commit()
logger.FatalErr(err)
tx, err = db.Begin(true)
logger.FatalErr(err)
bucket = tx.Bucket(name)
}
}
I get about 300 Kb/s speed and this is not even complete (as it's not adding tag to each word, only stores the last occurrence). So adding the array and JSON serialisation would definitely lower that speed...
So I gave mongodb a try
index := mgo.Index{
Key: []string{"word"},
Unique: true,
DropDups: false,
Background: true,
Sparse: true,
}
err = c.EnsureIndex(index)
logger.FatalErr(err)
line := ""
linesRead := 0
bulk := c.Bulk()
for scanner.Scan() {
line = scanner.Text()
data := strings.Split(line, ":")
bulk.Upsert(bson.M{"word": data[0]}, bson.M{"$push": bson.M{"tags": data[1]}})
linesRead += 1
if linesRead % 10000 == 0 {
_, err = bulk.Run()
logger.FatalErr(err)
bulk = c.Bulk()
}
}
And I get about 300 Kb/s as well (though Upsert and $push here handle appending to the list).
I tried with a local MySQL instance as well (indexed on word) but speed was like 30x slower...

FB_FileClose is only the first time busy

I'm working with TwinCAT 3 and ST to save data from a socket connection. The sockets work and parts of the saving too, but not all of it. The first array I try to save works fine. But if i want to save another one, it fails. The FB_FileClose doesn't go bBusy.
IF reset THEN // I reset the FB after it saved one array.
bSuccess := FALSE;
iError := 0;
step := 1;
reset := FALSE;
MEMSET(ADR(saveArray), 0, SIZEOF(saveArray));
RETURN;
END_IF
CASE step OF
1:
IF path = '' THEN
bSuccess := FALSE;
iError := 12;
step := 1;
END_IF
fbFileOpen.sPathName := path;
fbFileOpen.nMode := FOPEN_MODEAPPEND OR FOPEN_MODEPLUS;
fbFileOpen.bExecute := TRUE;
fbFileOpen.tTimeout := T#2S;
fbFileOpen();
step := 2;
2:
fbFileOpen(bExecute := FALSE);
IF NOT fbFileOpen.bBusy AND NOT fbFileOpen.bError THEN
step := 3;
ELSE
iError := fbFileOpen.nErrId;
END_IF
3:
fbWriteFile.hFile := fbFileOpen.hFile;
fbWriteFile.bExecute := TRUE;
fbWriteFile.pWriteBuff := ADR(saveArray);
fbWriteFile.cbWriteLen := SIZEOF(saveArray);
fbWriteFile.tTimeout := T#2S;
fbWriteFile();
step := 4;
4:
fbWriteFile(bExecute := FALSE);
IF NOT fbWriteFile.bBusy AND NOT fbWriteFile.bError THEN
step := 5;
END_IF
5:
fbCloseFile.hFile := fbFileOpen.hFile;
fbCloseFile.bExecute := TRUE;
fbCloseFile.tTimeout := T#3S;
fbCloseFile();
IF fbCloseFile.bBusy THEN //Gets suck here at the second run. And if I remove it, the FB doesn't get busy and doesn't close my hFile.
step := 6;
END_IF
6:
fbCloseFile(bExecute := FALSE);
IF NOT fbCloseFile.bBusy AND NOT fbCloseFile.bError THEN
bSuccess := TRUE;
ELSE
iError := fbCloseFile.nErrId;
END_IF
END_CASE
I also noticed that FB_FileOpen opens the same hFile two times in a row. The second one of it can't get closed from the FB_FileClose. The next run it gets a new hFile and then it can save the data. Next one it can't, and so on. What's my error in this one?
Thank you!
Found the solution by myself after a bit of hackaround. Before I set all the parameters for FB_FileOpen, FB_FileWrite and FB_FileClose I set the execute to false like this:
1:
IF path = '' THEN
bSuccess := FALSE;
iError := 12;
step := 1;
END_IF
fbFileOpen(bExecute := FALSE); // Set this to false before.
fbFileOpen.sPathName := path;
fbFileOpen.nMode := FOPEN_MODEAPPEND OR FOPEN_MODEPLUS;
fbFileOpen.bExecute := TRUE;
fbFileOpen.tTimeout := T#2S;
fbFileOpen();
step := 2;
Now it's working.

Exception raised when setting Text property of TEdit in custom component (Lazarus)

Using: Lazarus 1.2.0; Windows 32-bit application
I have created a custom component derived from TCustomPanel and contains some TEdit controls.
At runtime, when I try to set the Text property of an edit control in my component, I get a runtime error.
This is the error:
Project project1 raised exception class 'External: SIGSEGV'.
In file '.\include\control.inc' at line 3246:
GetTextMethod := TMethod(#Self.GetTextBuf);
I Googled and could not find anybody else reporting this error specifically when setting the Text property of TEdit.
This leads me to believe that I did something wrong when writing the component. Please check my code and point out what is wrong and how to fix it. TIA!
Code follows:
unit uEditPanel;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
type
{ TEditPanel }
TEditPanel = class(TCustomPanel)
Edit0: TCustomEdit;
Edit1: TCustomEdit;
Edit2: TCustomEdit;
Edit3: TCustomEdit;
Edit4: TCustomEdit;
private
{ Private declarations }
function GetEdit0Text: string;
procedure SetEdit0Text(AText: string);
function GetEdit1Text: string;
procedure SetEdit1Text(AText: string);
function GetEdit2Text: string;
procedure SetEdit2Text(AText: string);
function GetEdit3Text: string;
procedure SetEdit3Text(AText: string);
function GetEdit4Text: string;
procedure SetEdit4Text(AText: string);
protected
{ Protected declarations }
public
{ Public declarations }
procedure CreateWnd; override;
published
{ Published declarations }
property Edit0Text: string read GetEdit0Text write SetEdit0Text;
property Edit1Text: string read GetEdit1Text write SetEdit1Text;
property Edit2Text: string read GetEdit2Text write SetEdit2Text;
property Edit3Text: string read GetEdit3Text write SetEdit3Text;
property Edit4Text: string read GetEdit4Text write SetEdit4Text;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TEditPanel]);
end;
{ TEditPanel }
function TEditPanel.GetEdit0Text: string;
begin
Result := Edit0.Text;
end;
procedure TEditPanel.SetEdit0Text(AText: string);
begin
Edit0.Text := AText;
end;
function TEditPanel.GetEdit1Text: string;
begin
Result := Edit1.Text;
end;
procedure TEditPanel.SetEdit1Text(AText: string);
begin
Edit1.Text := AText;
end;
function TEditPanel.GetEdit2Text: string;
begin
Result := Edit2.Text;
end;
procedure TEditPanel.SetEdit2Text(AText: string);
begin
Edit2.Text := AText;
end;
function TEditPanel.GetEdit3Text: string;
begin
Result := Edit3.Text;
end;
procedure TEditPanel.SetEdit3Text(AText: string);
begin
Edit3.Text := AText;
end;
function TEditPanel.GetEdit4Text: string;
begin
Result := Edit4.Text;
end;
procedure TEditPanel.SetEdit4Text(AText: string);
begin
Edit4.Text := AText;
end;
procedure TEditPanel.CreateWnd;
begin
inherited CreateWnd;
Caption := EmptyStr;
Height := 117;
Width := 289;
BevelOuter := bvNone;
ClientHeight := 117;
ClientWidth := 289;
Edit0 := TCustomEdit.Create(Self);
Edit1 := TCustomEdit.Create(Self);
Edit2 := TCustomEdit.Create(Self);
Edit3 := TCustomEdit.Create(Self);
Edit4 := TCustomEdit.Create(Self);
Edit0.Left := 0;
Edit0.Height := 21;
Edit0.Top := 0;
Edit0.Width := 288;
//Edit0.BorderStyle := bsNone;
Edit0.TabOrder := 0;
Edit1.Left := 0;
Edit1.Height := 21;
Edit1.Top := 24;
Edit1.Width := 288;
// Edit1.BorderStyle := bsNone;
Edit1.TabOrder := 1;
Edit1.Font.Color := clGray;
Edit2.Left := 0;
Edit2.Height := 21;
Edit2.Top := 48;
Edit2.Width := 288;
// Edit2.BorderStyle := bsNone;
Edit2.TabOrder := 2;
Edit2.Font.Color := clGray;
Edit3.Left := 0;
Edit3.Height := 21;
Edit3.Top := 72;
Edit3.Width := 288;
//Edit3.BorderStyle := bsNone;
Edit3.TabOrder := 3;
Edit3.Font.Color := clGray;
Edit4.Left := 0;
Edit4.Height := 21;
Edit4.Top := 96;
Edit4.Width := 288;
//Edit4.BorderStyle := bsNone;
Edit4.TabOrder := 4;
Edit4.Font.Color := clGray;
Edit0.Parent := Self;
Edit1.Parent := Self;
Edit2.Parent := Self;
Edit3.Parent := Self;
Edit4.Parent := Self;
Edit0.SetSubComponent(True);
Edit1.SetSubComponent(True);
Edit2.SetSubComponent(True);
Edit3.SetSubComponent(True);
Edit4.SetSubComponent(True);
end;
end.
Solved. Answer posted by user JuhaManninen on the Lazarus support forum:
"You have no constructor in your class. Replace CreateWnd with a
constructor."

Array changes value from string to number

I'm trying to assign a hex color value to an array. This is not possible, because when I assign the value, it is changed to a number. Here is the code that does that:
settings := {myColor: "color"}
myColor := "color"
settingName := "myColor"
settingValue := "0x00FF00"
%settingName% := settingValue
settings[settingName] := settingValue
e := settings[settingName]
MsgBox %e% - %settingValue%
;Displays 65280 - 0x00FF00
I think you went wrong in the 5th code line. Variables that you define do not get % signs.
settings := {myColor: "color"}
myColor := "color"
settingName := "myColor"
settingValue := "0x00FF00"
settingName = %settingValue% ; Alternative 1
settingName := settingValue ; Alternative 2
settings[settingName] := settingValue
e := settings[settingName]
MsgBox % e " - " settings[settingName] " - " settingValue
;Displays 0x00FF00 - 0x00FF00 - 0x00FF00
ExitApp
Though I think that you want: Settings[MyColor] = 0x00FF00, you now have Settings[0x00FF00] = 0x00FF00.
settings := {myColor: "color"}
myColor := "color"
settingName := "myColor"
settingValue := "0x00FF00"
settings[settingName] := settingValue
MsgBox % settings[settingName] " - " settingValue
;Displays 0x00FF00 - 0x00FF00
ExitApp