I have the following problem:
I am using NodeJS with Express and MongoDB to query my database.
I have a document in the collection of "domains" containing the field "domain".
For example:
{
"domain" : "mydomain.com, www.mydomain.com, beta.mydomain.com, *.beta.mydomain.com",
"APIKeys" : [ "Public" : 111111 ]
}
Or another document:
{
"domain" : "example.com, *.example.com",
"APIKeys" : [ "Public" : 222222 ]
}
I would like to query the database and return the result if extractHostname(req.get('Referrer')) matches any of the domains in the field.
var collection = 'domains';
var query = { $and: [ { 'APIKeys.Public' : req.query.APIKey }, {'domain' : extractHostname(req.get('Referrer')) } ] };
var projection = { '_id' : 1 , 'playerPref' : 1 };
For example: extractHostname(req.get('Referrer')) = beta.mydomain.com it should return true, since it matches the regex of beta.mydomain.com.
'test.beta.mydomain.com' should return true since it matches the regex of *.beta.mydomain.com.
'test.www.mydomain.com' should return false.
'www.mydomain.com.maliciousdomain.com' should return false.
Any idea how I can make a query like this to check if the Referrer is in matching conditions?
The problem I am facing is that any of the strings in the field need to match the query, and not the other way around. Keeping in mind the wildcard in the field as opposed in the search string. (It is like a reserve regex?)
Kind regards,
Hugo
After seeing your updated requirements I've created a pattern that will match any of your various domain types. Here it is
(?(?=.* .*)(([^ \n>]*)(?:.*))|([^\w\W]))
It will always match mydomain.com, with an optional www., optional beta. and optional wildcard.
I have a file which has following data. I just want the ownerId numbers and the profileID values separated by :.
My file:
ObjectId("57a046a06f858a9c73b3468a"), "ownerId" : "923003345778", "profileId" : "FreeBundles,LBCNorthParentOffer", "instanceId" : null, "queuedFor" : "unassigned", "state" : "active", "createDateTime" : 1470121632, "startDateTime" : 1470121632, "expireDateTime" : 1485673632, "removeDateTime" : 1487747232, "extensionDateTime" : null, "cancelled" : false, "mode" : "onceOff", "nextMode" : "none", "profileData" : { "serviceProfileId" : "ecs19", "counter" : 1 } }
ObjectId("57a046a06f858a9c73b34688"), "cancelled" : false, "createDateTime" : 1470121632, "expireDateTime" : 1557514799, "extensionDateTime" : null, "instanceId" : null, "mode" : "onceOff", "nextMode" : "none", "ownerId" : "923003345778", "profileData" : { "serviceProfileId" : "ecs19", "counter" : 1 }, "profileId" : "Prov3G,HLRProv", "queuedFor" : "unassigned", "removeDateTime" : 1557514799, "startDateTime" : 1470121632, "state" : "active" }
ObjectId("56d48bd38a8b93baa708fcfa"), "ownerId" : "923003309452", "profileId" : "DiscountOnUsage,Segment04", "instanceId" : null, "queuedFor" : "unassigned", "state" : "active", "createDateTime" : 1456770003, "startDateTime" : 1456770003, "expireDateTime" : null, "removeDateTime" : null, "extensionDateTime" : null, "cancelled" : false, "mode" : "onceOff", "nextMode" : "none", "profileData" : { "serviceProfileId" : "ecs19", "counter" : 1 } }
ObjectId("560ed95f6ca6e0703cf26fcc"), "cancelled" : false, "createDateTime" : 1443813727, "expireDateTime" : 1544381999, "extensionDateTime" : null, "instanceId" : null, "mode" : "onceOff", "nextMode" : "none", "ownerId" : "923003309452", "profileData" : { "serviceProfileId" : "ecs19", "counter" : 1 }, "profileId" : "Prov3G,HLRProv", "queuedFor" : "unassigned", "removeDateTime" : 1544381999, "startDateTime" : 1443813727, "state" : "active" }
Output:
923003345778 : FreeBundles,LBCNorthParentOffer
923003345778 : Prov3G,HLRProv
923003309452 : DiscountOnUsage,Segment04
923003309452 : Prov3G,HLRProv
Please also explain me in detail the answer if anyone knows.
$ sed 's/.*ObjectId("\([^"]*\).*"profileId" *: *"\([^"]*\).*/\1 : \2/' file
57a046a06f858a9c73b3468a : FreeBundles,LBCNorthParentOffer
57a046a06f858a9c73b34688 : Prov3G,HLRProv
56d48bd38a8b93baa708fcfa : DiscountOnUsage,Segment04
560ed95f6ca6e0703cf26fcc : Prov3G,HLRProv
I really don't think any explanation is needed as it's very straight forward but let me know if you have any questions.
This is a rather awkward situation you've managed to put yourself into.
As a rule, you do not want to handle structured data with plain-text tools like sed. Any solution you come up with will be brittle in the face of formatting changes (such as spaces or newlines between JSON fields), and certain corner cases (such as JSON strings with quotation marks in them) are awkward to handle with it. If you have JSON, you want to use a JSON tool to handle it.
However, you don't exactly have JSON there. This is a textual representation of BSON (likely from MongoDB) that has already had some parts chopped off.
What you really want to do
A sane way to solve this problem is to make MongoDB give you JSON and let something like jq do the formatting. Once you have a proper JSON file, this will be as simple as
jq -r '"\(.ownerId) : \(.profileId)"' file.json
mongoexport may be your friend here, or putting JSON.stringify() around your query in the MongoDB shell1; it depends on how you got this data in the first place. This approach will require you to save the unchopped data, but anyway I suspect that whatever made you chop the BSON into pieces should be replaced with something similar to improve reliability.
1If you got the data from the MongoDB shell, you may want to consider doing the formatting there, though.
How to hack yourself deeper into this mess with sed
However, since you don't currently have proper JSON, you may want to try to hack yourself out of this mess with sed. This is a terrible idea, and I cannot stress enough that you never ever want to do this in a production environment. If you do, you'll be in a deeper mess than before, and that sort of vicious cycle is not a happy place to be.
So, what I'm about to show you is the sort of thing that you do as a one-off in a hurry and are never going to use again because you promise yourself to do it properly next time. You want to check the results carefully. Here goes:
sed 'h;/^.*"profileId"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/!d;s//\1/;x;/^.*"ownerId"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/!d;s//\1/;G;s/\n/ : /' file.bsonish
This makes the following assumptions about the input data:
One full object per line. Newlines in the wrong place will break this.
No " in either the ownerId or the profileID field
Furthermore, it will not recognize broken data, which is always a nice feature. On the upside, it does not require the ownerId and profileId fields to appear in any particular order.
It works as follows:
# Save a copy of the input data; we'll isolate the fields separately.
h
# See if there's a profileId field. If not, the line is silently dropped.
/^.*"profileId"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/!d
# Isolate that profileId field. // in this context means: reuse the last
# regex (the big one)
s//\1/
# Now swap in the saved input data. We'll get ownerId next.
x
# Isolate ownerId as before. If there is no ownerId field, drop line silently.
/^.*"ownerId"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/!d
s//\1/
# append profileId field in hold buffer to what we have
G
# Replace the newline between the two with a colon and some spaces.
s/\n/ : /
According to this How to update a field type in elasticsearch, I have added Put Mapping AP. However Kibana, I get property as unknown (See screenshot below). In rank field, there's numeric values or 0.
{
"tweet" : {
"properties" : {
"user" : {"type" : "string", "index" : "not_analyzed"},
"message" : {"type" : "string", "null_value" : "na"},
"postDate" : {"type" : "date"},
"priority" : {"type" : "integer"},
"rank" : {"type" : "float"}
}
}
}
Refresh Field list, fix the problem
Any fields added later need refresh. To do so:
Go to Kiabana
Go to Management and select Index.
Click on Refresh Field List.
I have a document structure like this one.
> db.urls.find()
{
"_id" : ObjectId("53d79c7020ba271c80b78b6c"),
"url" : "http://www.newstoday.com.bd?option=details&news_id=2368296&date=2014-01-27///",
"priority" : 0.25,
"date" : ISODate("2014-07-29T13:06:58.745Z"),
"seen" : 1
}
To find some document using regex I used the following,
> db.urls.find({url: { $regex: 'http://www.newstoday.com.bd?option='} })
>
Which resulted empty. I need some help on the proper regex to use here.
(?=.*?http:\/\/www\.newstoday\.com\.bd\?.*)(.*)
This will give the document based on the url if that is what you are looking for.
See Demo.
http://regex101.com/r/wE3dU7/1
How to use nin and regex in mongoDB?
I want to find document using nin and regex
but nin does not work!
Query:
{ "$and" : [
{ "id" : { "$nin" : [ "529653cb5bc5b0e42d339bd3" , "529653cb5bc5b0e498339bd3"]}} ,
{ "content" : { "$regex" : "(?i)apple" , "$options" : "i"} }
] }
Should I using mongo subquery?
Your problem could be multiple things depending upon the error you're getting.
But a quick examination of your query suggests it could be your use of the "id" field. The primary key field in all documents is "_id". Your query uses the field "id" but you're probably trying to query the field "_id".