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".
Related
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.
In my sandbox I have a collection, and the unique key (_id) for the collection is a unique string from another database. I have preallocated the documents and they look like this
The data looks like this
{ _id : "UNIQUEKEY1:1463670000000", data: {value:NaN} }
{ _id : "UNIQUEKEY2:1463670000000", data: {value:NaN} }
I would like to query the data in the following way
{ "_id": {$regex : "/^UNIQUEKEY1.*/i"} }
I have read that you can query _id if it is a string in Brendan's comment here
I don't want the overhead of another attribute just to search by when the _id would provide me with enough
It's a valid setup and $regex should work fine (see https://docs.mongodb.com/manual/reference/operator/query/regex/)
So try db.mycollection.find({ "_id": {$regex : /^UNIQUEKEY1.*/i} }) i.e. you shouldn't need the quote marks.
I have a mongo collection User which contains data like:-
{
id : 1,
name : "gaurav",
skills : "C++ HTML CSS"
}
when I am searching for all users that have C++ skill in it with the following query I am getting correct results as expected
db.user.find({skills:{contains:"C++"}});
But when I am searching all the unique names from the user using the same condition I m not getting any desired result
db.user.distinct('name',{skills:{contains:"C++"}});
Can anyone help me with what I am doing wrong?
The "contains" is not a valid keyword for MongoDB queries. You need $regex which submits a general "regular expression" statement matching the pcre specifications:
db.user.distinct( "name", { "skills": { "$regex": "C\+\+" } })
If using JavaScript as you language then this is also safe:
db.user.distinct( "name", { "skills": /C\+\+/ })
To determine if the string "C++" occurred somewhere within the string value of the field being tested. The + character is reserved in "regex" operations and therefore you need to escape it with a \ char as the standard escaping mechanism.
On your data this is the result:
db.user.distinct( "name", { "skills": { "$regex": "C\+\+" } })
[ "gaurav" ]
Try to use REGEX like below query
db.user.distinct("name",{"skills":{"$regex":"C++.*"}})
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
I use MongoDB. I create a database(500 000 000 documents) and collection in it for testing reasons. All documents look like bellow:
{
"_id" : ObjectId("50c1fbcda8cf8e11c43ea8ce"),
"sql_id" : 8311,
"text" : "WD7TYIM0H H3Q 3874 000 VFBF6H",
"xml" : "<root> <tag_0>WD7TYIM0H</tag_0> <tag_1>H3Q</tag_1> <tag_2>3874</tag_2><tag_3>000</tag_3><tag_4>VFBF6H</tag_4></root>",
"tags" : [
"WD7TYIM0H",
"H3Q",
"3874",
"000",
"VFBF6H"
]
}
I create index by field "tags" and want to use multiple regexp condition that uses "tags" index. Is this possible?
I tried:
> db.items.find({ "$and" : [{ "tags" : /^AAA/ }, { "tags" : /^BBB/ }] })
> db.items.find({ "tags" : { "$all" : [/^AAA/, /^BBB/] } })
twice Mongo went down.
If I search by one condition result returns very fast.
Thanks!
In my test it goes well. I'm using db version v1.6.5. Given the fact that I have only a few documents in DB, the problem may be a performance-related one.