Is it possible to search within a MongoDB field with type of Date or Int? - mongodb

I have a search field within my application that I want users to enter a search term into and it searches across various fields in each Mongo document. Naturally, I can search data within the fields that are of type String (currently using a regular expression), but how do I do this for those with type Date or type Int that?
Note: when I say search within the field, I mean if a user types '16' into the search field, it will return dates that contain '16', e.g. 01/01/2016 or 16/03/2014. Same principle for integers.

One quick way I think is you can use $where
value to search val = "16"
db.foo.find({$where : "function(){ if(this.dateField.toString().indexOf(val)>= 0 || (""+this.intField).indexOf(val)>=0){return true;}}"})
What basically you can try is convert the field value into string and then search it in there. Downside is $where doesn't uses index, it basically scans the collection, You cannot use other operators when you are using $where.

Yes it is possible.
You can tell your find() to look into only fields which are of specific data types.
$type comes handy.
check out the following link for examples and usage.
https://docs.mongodb.com/manual/reference/operator/query/type/
an example would be
db.addressBook.find({"field": "search-value"}, { "field" : { $type : "double" } } )
will return documents where search-value has a match as well as if the field is of type double.

since i have an image hence posting it as another answer rather than a comment.
now if you notice, documents with ID 2 and ID 6 are exactly the same. Only difference is the data type for the zipcode field.
i ran my query with $and as you can see and it bring backs only the matching record. if i get rid of the $and and the $type condition it will bring back both the records.
i hope this will help you solve your issue.

Related

Find documents with a certain field value only when another field value is a given string

I'm using this php package to make queries - https://github.com/jenssegers/laravel-mongodb
The situation is, there are two fields, user_id and post_status among others. I want to retrieve all the documents in that collection, but when post_status field value is draft, that should be retrieved only when user_id is a given string. The idea is, only logged in user finds their drafted posts among other posts.
I'm having hard time finding any solution for this problem. The app is still not in production. If I should store data is some different manner, that is an option as well.
Find documents with a certain field value only when another field value is a given string
The question your are framing is simply convert into a and query, how let's see it
when another field value is a given string
This means that you have some result sets and you need to filter out when user_id match with some string. i.e some result sets and user_id = <id>
Now consider the first part of the sentence Find documents with a certain field value
This means you are filtering the records with some values i.e "status" = "draft" and whatever result will come and want again to filter on the basis of user_id = <id>
So finally you will end-up with below query:
db.collectionName.find({"status":"draft", "user_id": "5c618615903aaa496d129d90"})
Hope this explanation will help you out or you can rephrase your question I will try to modify by ans.

Can't find documents by criteria containing string objectId value

I have a collection list such:
{username: 'somename',
friendId: '57d725d6b8b144044602bf74' <-- This a reference objectId to another doc
}
When I query docs in my collection with criteria {friendId : '57d725d6b8b144044602bf74'} I get no results back .
Any other field query works fine.
I tried to convert the value to ObjectId('57d725d6b8b144044602bf74') even though the value is just a string, still no go.
Why am I failing to search for by that type of string ?
you are trying to achieve 'self-join' in mongoDB and you seems to rely on the _id field generated by mongodb.
i would suggest you to supply custom _ID fields to the document.
eg:
{_id:"alex", name:"alex", friendID:""}
{_id"john", name"john", friendID:"alex"}
and then you can execute your queries with ease on friendID field. Give it a shot and see if this make sense for your requirement.

Distinguish array from single value in a document

I have two type of documents in a mongodb collection:
one where key sessions has a simple value:
{"sessions": NumberLong("10000000000001")}
one where key sessions has an array of values.
{"sessions": [NumberLong("10000000000001")]}
Is there any way to retrieve all documents from the second category, ie. only documents whose value is an arary and not a simple value?
You can use this kind of query for that:
db.collectionName.find( { $where : "Array.isArray(this.sessions)" } );
but you'd better convert all the records to one type to keep the things consistent.
This code can be simple like this:
db.c.find({sessions:{$gte:[]}});
Explanation:
Because you only want to retrieve documents whose sessions data type is array, and by the feature of $gte (if data types are different between tow operands, it returns false; Double, Integer32, Integer64 are considered as same data type.), giving an empty array as the opposite operand will help to retrieve all results by required.
Also , $gt, $lt, $lte for standard query (attention: different behaviors to operaors with same name in expression of aggregation pipeline) have the same feature. I proved this by practice on MongoDB V2.4.8, V2.6.4.

Indexing and searching number field in mongodb

I have created the following collection and text index:
db.test1.insert({"name":"kumar goyal","email":'rakesh.goyal#gmail.com',"phoneNo":9742140651});
db.test1.ensureIndex({ "$**": "text" },{ name: "TextIndex" })
Partial search is working for name and email field e.g.
db.test1.runCommand("text",{search:'rakesh'});
returns the record properly but its not working on the phoneNo field.
db.test1.runCommand("text",{search:'9742'});
is not working.
I guess text index is not working on number fields. Is there anyway to make it work in mongodb?
You are inserting the telephone number as a number, but then searching for it as a string.
Perhaps you should consider inserting the telephone number as a string. You're not planning to perform any arithmetic actions on that field are you?
This way mongo will be able to perform textual searches (like the one you gave as an example) on the "numbers" but will treat them as strings.
Mongo DB full text does not support partial search. Hence it was not working, nothing to do with numbers.

Mongo - dot notation works like $or? Can't provide multiple parameters?

I tried to update an existing document with two dot notation parameters, my query:
{ _id: "4eda5...", comments._id: "4eda6...", comments.author: "john" }
my update was:
{ "comments.$.deleted": true }
However, weirdly enough, when I passed a non-existent combination of comment id+author, it just updated the first matching comment by that author.
Any ideas why that's happening?
EDIT: C# Code sample
var query = Query.And(Query.EQ("_id", itemId), Query.EQ("cmts._id", commentId));
if (!string.IsNullOrEmpty(author))
query = Query.And(query, Query.EQ("cmts.Author", author));
var update = Update.Set("cmts.$.deleted", true);
var result = myCol.Update(query, update, UpdateFlags.None, SafeMode.True);
You want $elemMatch if you want the _id and author to be in the same comment. Really, your query doesn't make much sense including the author as the id should be as unique as you can get, no?
It is based on the first matching array element which replaces the "$" in for the update.
This is working by design. It is similar to an or since it can find a document which both has the _id and an author that match in any of the array elements.
The query is not working the way you are expecting it to. Basically, when using the $ positional notation you need to make sure that your query only has one clause that queries an array, otherwise it is ambiguous which of the two array comparisons the $ should refer to.
In your case, you are asking for a document where:
The _id is equal to some value
The comments array contains some document where the _id is equal to some value
The comments array contains some document where the author is equal to some value
Nothing in your query says that 2. and 3. need to be satisfied by the same document.
So even though you are using a non-existent combination of comment._id and comment.author, your comment array does have at least one entry where the _id is equal to your search value and some other entry (just not the same one) where the author is equal to your search value.
Since the author was the last one checked, that's what set the value of the $, and that's why that array element got updated.