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

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/})

Related

Searching MongoDB with Go [duplicate]

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.

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/

mongodb console update change data type [duplicate]

This question already has answers here:
Change the type of a MongoDB field to integer from its console
(3 answers)
Closed 8 years ago.
The original data is:
{"u":1}
The type of 'u' is Int.
After I run command in mongod console:
update({},{$set:{"u":0}})
find({})
data indeed become
{"u":0}
looks pretty ok. But when I use C++ driver to read them:
bson.getIntField("u")
Crash. The reason is type 'u' is Double! That means mongod's update command change u's type soundlessly.
Why? and how to prevent this?
P.S
mongodb version is 2.6.6 linux
Any numeric value is inserted or "changed" as a Double by default. For other "types" use the NumberInt() or NumberLong() for the respective type you expect to read in your C++ or other type sensitive code:
update({},{ "$set":{ "u": NumberInt(0) }})
or:
update({},{ "$set":{ "u": NumberLong("0") }})

search function in mongoDB with case-insensitive query [duplicate]

This question already has answers here:
case insensitive find in mongodb for usernames in php
(2 answers)
Closed 8 years ago.
I am doing with function in mongoDB, now it's have problem with case-insensitive data. this is my code in function
$where = array(TblFact::Fou_Name => array('$regex' =>$SearchNameFactory));
this code when data in uppercase and i search by lowercase is return null. So anyone can help me to find solution for case-insensitive query ?
I am looking to see your replay soon. Thanks ...
Thanks everyone for help me , now my problem have been resolve by
$where = array(TblFact::Fou_Name => new MongoRegex("{$SearchNameFactory}/i"));
hope it can help to anyone who meet problem like me
thanks

Structure for a Questions - Answers Collections

I'm not proficient in MongoDB and I have short question. What is the best structure for Questions - Answers Collections. Should I separate them to different collections or keep Answers as a property of Question? To give you more details:
Each question can have many Answers
I don't need to search in Answers
I will search in Questions
Each Answer will have userId, votes and of course answer properties
After finding Question, I will be able to expand Answers list and vote for the Answer
Questions will be also votable
It will be something like stackoverflow but much simpler. Thanks in advance.
I would keep them in two separated collections. As you said you have to be able to query in the questions collection so make a questions collection, you only have to show up answers realted for a specific selected question but will have some specific fields as you mentioned like votes. If we think about stackoverflow for example, you see the questions : click on the question --> then you can query an answers collection for the answer related to that question, also for answers you can have comments which if you not expect insane much of them can kept in a list along with the answer.You can keep the answers of the questions in a collection with a reference and an index on the question id.
Something like his one:
questions:
{
"_id" : ObjectId("52205df3d6c893638bc2aa14"),
"q" : "What is hip?",
"v" : 1000
}
answers :
{
"_id" : ObjectId("52205e72d6c893638bc2aa15"),
"q_id" : ObjectId("52205df3d6c893638bc2aa14"),
"a" : "Tell me, tell me!",
"u" : "uid",
"v" : 153,
"c" : [
{
"c" : "Cool",
"u" : "uid",
"v" : 100
}
]
}