Searching MongoDB with Go [duplicate] - mongodb

This question already has answers here:
Partial matches using mongo's primitive package
(2 answers)
Find entries via substring regex query in mongodb-go-driver
(2 answers)
Closed 11 months ago.
I've been learning go and I am able to work with some basic MongoDB functions via the mongo-driver I am unable to figure out the search functions. I believe this is due to the interpreter adding quotes around my search.
searchQuery := "/" + params.Get("q") + "/"
filter := bson.D{
{"$and",
bson.A{
bson.D{
{"title", searchQuery},
},
},
},
}
cur, currErr := collection.Find(ctx, filter)
I am expecting to add a partial string to the query string and have return all of the documents that match. If I could query the search index i've added that would be even better. Either way I am unable to get it to work.

Related

MongoDB text search finds the long string, but not the shorten string? [duplicate]

This question already has answers here:
MongoDB Full and Partial Text Search
(11 answers)
Closed 3 years ago.
In MongoDB 3.6 I have a collection of data with a text search index. It can find the longer version of a word, but not a shorter version, how can I make it find both versions?
db.test.createIndex({name: 'text', description: 'text'});
db.test.insert({name: 'MYREALLYLONGNAME', description: 'MYREALLYLONGNAME'});
db.test.find({$text: {$search: 'MYREALLYLONGNA'}});
> FINDS IT
db.test.find({$text: {$search: 'MYREALLYL'}});
> DOES NOT FIND IT
That would have to be:
db.test.find({{$text: {$search: /.*MYREALLYL.*/}})
db.test.find( { name: { $regex: /MYREALLYL/ } } )
note: mongodb uses regular expressions which are more powerful than
"LIKE" in sql. With regular expressions you can create any pattern
that you imagine.
For more info on regular expressions refer to this link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
https://docs.mongodb.com/v3.6/reference/operator/query/regex/

Creating a filter in mongo-go-driver for .FindOne

I'm trying to check a collection to see if there is at least one documents that match a specific set of values.
I've tried reading the documentation at https://github.com/mongodb/mongo-go-driver#usage, but I can't seem to find much help there. I'm pretty new to MongoDB & Go, I believe that this is more a problem of my lack of experience.
Here is a sample query from Studio 3T that I'm trying to run with mongo-go-driver:
db.getCollection("events").find(
{
"event.eventType" : "OSR",
"context.vehicleId" : NumberInt(919514),
"ts" : {
"$gte" : ISODate("2019-06-21T21:38:43.022+0000")
}
}
).limit(1);
It seems that the context.FindOne method will do what I want (and eliminating the need for the .limit(1)). I thought that it would be straight forward to "port" this to Go and the mongo-go-driver.
I can sort of make this work, for example I have the following which will find me all the OSR:
var query = &bson.D{
{"event.eventType", "OSR"},
}
result := bson.D{}
e := collection.FindOne(context.TODO(), query).Decode(&result)
This will return me one document. Now, if I want to include the vehicleId value, and I update the query to:
var query = &bson.D{
{"event.eventType", "OSR"},
{"context.vehicleId", 919514},
}
No documents are returned. I haven't bother to expand query to include the ts field yet.
I would expect at to still have at least one document returned, but nothing is showing up. Does anybody have some tips, suggestions or guidance on what I'm doing wrong (or perhaps how I can do this better)?
Not quite sure, but have you tried with bson.M instead of bson.D?
It seems like it's working for me at least.
query := &bson.M{
"event.eventType": "OSR",
"context.vehicleId": 919514,
}
Please refer to the docs for more information.
Also, like #owlwalks said, are you sure, you're in the right collection?

Lucene.NET version 4.8 beta casing issue [duplicate]

This question already has answers here:
Java Lucene 4.5 how to search by case insensitive
(3 answers)
Closed 3 years ago.
I'm using Lucene.NET version 4.8 (beta) for a little search task in a solution I'm doing, but have problems searching case insensitive. I know that Lucene isn't case insensitive, but when using the StandardAnalyzer, it should lowercase the data stored (according to the documentation here StandardAnalyzer), as long as you make sure the queries are done right.
So any idea what I'm doing wrong here? I've stored the data "Kirsten" in a field in 4 different documents, and when searching for (lowercased) "kirsten" I get no hits, but when searching for "Kirsten" I get the expected 4.
Here's my query code:
query = query.ToLowerInvariant();
BooleanQuery q = new BooleanQuery {
new BooleanClause(new WildcardQuery(new Term(FieldNames.Name, query + WildcardQuery.WILDCARD_STRING)), Occur.SHOULD),
new BooleanClause(new WildcardQuery(new Term("mt-year", query)), Occur.SHOULD),
new BooleanClause(new WildcardQuery(new Term("mt-class", query + WildcardQuery.WILDCARD_STRING)), Occur.SHOULD)
};
And the issue is that the users would always write the lowercase version, and expect it to find both lower- and upper-case.
As #Peska wrote in the comments, this was a case of using StringField instead of TextField when adding the document (and data) to Lucene.
Once I switched to using TextField, everything worked as expected.

How to write query with LIKE in MongoDB? [duplicate]

This question already has answers here:
How to query MongoDB with "like"
(45 answers)
Closed 4 years ago.
The document looks like:
{
"_id" : ObjectId("5c11389782c3c3751def6a68"),
"\"dname\"" : "\"W6PJBu7wC0Demyl86pd8Um5NHk3ukqsdtVA6 \""
}
I've searched a lot and found that I can write it as: /. For example, I need to get all document where dname starts with c, for this I do:
db.getCollection('flat').find({"\"dname\"" : "\"c/"})
Also I've tried this:
db.getCollection('flat').find({"\"dname\"" : "/^\"c/"})
but it does not work. What I do wrong?
You need to use regexes
For example, to find records with dname starting with c: db.getCollection('flat').find({"\"dname\"" : /^\"c/})

MongoDB - find if a field value is contained in a given string

Is it possible to query documents where a specific field is contained in a given string?
for example if I have these documents:
{a: 'test blabla'},
{a: 'test not'}
I would like to find all documents that field a is fully included in the string "test blabla test", so only the first document would be returned.
I know I can do it with aggregation using $indexOfCP and it is also possible with $where and mapReduce. I was wandering if it's possible to do it in find query using the standard MongoDB operators (e.g., $gt, $in).
thanks.
I can think of 2 ways you could do this:
Option 1
Using $where:
db.someCol.find( { $where: function() { return "test blabla test".indexOf(this.a) > -1; } }
Explained: Find all documents whose value of field "a" is found WITHIN some given string.
This approach is universal, as you can run any code you like, but less recommended from a performance perspective. For instance, it cannot take advantage of indexes. Read full $where considerations here: https://docs.mongodb.com/manual/reference/operator/query/where/#considerations
Option 2
Using regex matching trickery, ONLY under certain circumstances; below is an example that only works with matching that the field value is found as a starting substring of the given string:
db.someCol.find( { a : /^(t(e(s(t( (b(l(a(b(l(a( (t(e(s(t)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?)?$/ } )
Explained: Break up the components of your "should-be-contained-within" string and match against all sub-possibilities of that with regex.
For your case, this option is pretty much insane, but it's worth noting as there may be specific cases (such as limited namespace matching), where you would not have to break up each letter, but some very finite set of predetermined parts. And in that case, this option could still make use of indexes, and not suffer the $where performance pentalties (as long as the complexity of the regex doesn't outweigh that benefit :)
You can use regex to search .
db.company.findOne({"companyname" : {$regex : ".*hello.*"}});
If you are using Mongo v3.6+, you can use $expr.
As you mentioned $indexOfCP can be used to get index, here it will be
{
"$expr": {
{$ne : [{$indexOfCP: ["test blabla test", "$a"]}, -1]}
}
}
The field name should be prefixed with a dollar sign ($), as $expr allows filters from aggregation pipeline.