pymongo - Search collection for documents that contain with $and operator case insensitive [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • How to AND and NOT in MongoDB $text search

    1 answer




I am trying to search a collection which contains documents, collection looking like this:



[{'description':'Fast and Dangerous',
'colour':'blue',
'make':'ford'},
{'description':'slow and dangerous',
'colour':'red',
'make':'lexus'}]


I am trying to build a search query that will return all the documents which contain Red and Dangerous (case insensitive)



I started off with:



find({"$text": {"$search": "red dangerous"}})


However, this is certainly case insensitive, but it is an OR rather than AND.



I have read through https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and - but it means you have to refer to specific fields which could contain Red or Dangerous, rather than the entire document. What's the best way to go about this? I want my code to be professional, and I feel whatever hack I come up with really won't be. Any pointers?










share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Use $meta aggregation with the $sort stage. Something like db.collection.aggregate( [ { $match: { $text: {"$search": "red Dangerous"} } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: 1 } } ] ). The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.
    – Anthony Winzlet
    Nov 11 at 15:00










  • That's a really interesting answer. Certainly highlighted some features I wasn't aware of. I'll put it in. It feels like my request should have a definite answer though, as in all of the requirements of the search have been met
    – rlou
    Nov 11 at 15:45















up vote
0
down vote

favorite













This question already has an answer here:




  • How to AND and NOT in MongoDB $text search

    1 answer




I am trying to search a collection which contains documents, collection looking like this:



[{'description':'Fast and Dangerous',
'colour':'blue',
'make':'ford'},
{'description':'slow and dangerous',
'colour':'red',
'make':'lexus'}]


I am trying to build a search query that will return all the documents which contain Red and Dangerous (case insensitive)



I started off with:



find({"$text": {"$search": "red dangerous"}})


However, this is certainly case insensitive, but it is an OR rather than AND.



I have read through https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and - but it means you have to refer to specific fields which could contain Red or Dangerous, rather than the entire document. What's the best way to go about this? I want my code to be professional, and I feel whatever hack I come up with really won't be. Any pointers?










share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Use $meta aggregation with the $sort stage. Something like db.collection.aggregate( [ { $match: { $text: {"$search": "red Dangerous"} } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: 1 } } ] ). The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.
    – Anthony Winzlet
    Nov 11 at 15:00










  • That's a really interesting answer. Certainly highlighted some features I wasn't aware of. I'll put it in. It feels like my request should have a definite answer though, as in all of the requirements of the search have been met
    – rlou
    Nov 11 at 15:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • How to AND and NOT in MongoDB $text search

    1 answer




I am trying to search a collection which contains documents, collection looking like this:



[{'description':'Fast and Dangerous',
'colour':'blue',
'make':'ford'},
{'description':'slow and dangerous',
'colour':'red',
'make':'lexus'}]


I am trying to build a search query that will return all the documents which contain Red and Dangerous (case insensitive)



I started off with:



find({"$text": {"$search": "red dangerous"}})


However, this is certainly case insensitive, but it is an OR rather than AND.



I have read through https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and - but it means you have to refer to specific fields which could contain Red or Dangerous, rather than the entire document. What's the best way to go about this? I want my code to be professional, and I feel whatever hack I come up with really won't be. Any pointers?










share|improve this question
















This question already has an answer here:




  • How to AND and NOT in MongoDB $text search

    1 answer




I am trying to search a collection which contains documents, collection looking like this:



[{'description':'Fast and Dangerous',
'colour':'blue',
'make':'ford'},
{'description':'slow and dangerous',
'colour':'red',
'make':'lexus'}]


I am trying to build a search query that will return all the documents which contain Red and Dangerous (case insensitive)



I started off with:



find({"$text": {"$search": "red dangerous"}})


However, this is certainly case insensitive, but it is an OR rather than AND.



I have read through https://docs.mongodb.com/manual/reference/operator/query/and/#op._S_and - but it means you have to refer to specific fields which could contain Red or Dangerous, rather than the entire document. What's the best way to go about this? I want my code to be professional, and I feel whatever hack I come up with really won't be. Any pointers?





This question already has an answer here:




  • How to AND and NOT in MongoDB $text search

    1 answer








python python-3.x mongodb pymongo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 14:17

























asked Nov 11 at 14:01









rlou

485




485




marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 8:23


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Use $meta aggregation with the $sort stage. Something like db.collection.aggregate( [ { $match: { $text: {"$search": "red Dangerous"} } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: 1 } } ] ). The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.
    – Anthony Winzlet
    Nov 11 at 15:00










  • That's a really interesting answer. Certainly highlighted some features I wasn't aware of. I'll put it in. It feels like my request should have a definite answer though, as in all of the requirements of the search have been met
    – rlou
    Nov 11 at 15:45


















  • Use $meta aggregation with the $sort stage. Something like db.collection.aggregate( [ { $match: { $text: {"$search": "red Dangerous"} } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: 1 } } ] ). The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.
    – Anthony Winzlet
    Nov 11 at 15:00










  • That's a really interesting answer. Certainly highlighted some features I wasn't aware of. I'll put it in. It feels like my request should have a definite answer though, as in all of the requirements of the search have been met
    – rlou
    Nov 11 at 15:45
















Use $meta aggregation with the $sort stage. Something like db.collection.aggregate( [ { $match: { $text: {"$search": "red Dangerous"} } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: 1 } } ] ). The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.
– Anthony Winzlet
Nov 11 at 15:00




Use $meta aggregation with the $sort stage. Something like db.collection.aggregate( [ { $match: { $text: {"$search": "red Dangerous"} } }, { $addFields: { score: { $meta: "textScore" } } }, { $sort: { score: 1 } } ] ). The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.
– Anthony Winzlet
Nov 11 at 15:00












That's a really interesting answer. Certainly highlighted some features I wasn't aware of. I'll put it in. It feels like my request should have a definite answer though, as in all of the requirements of the search have been met
– rlou
Nov 11 at 15:45




That's a really interesting answer. Certainly highlighted some features I wasn't aware of. I'll put it in. It feels like my request should have a definite answer though, as in all of the requirements of the search have been met
– rlou
Nov 11 at 15:45












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can use $meta aggregation with the $sort stage. Something like



db.collection.aggregate([
{ "$match": { "$text": { "$search": "red Dangerous" } } },
{ "$addFields": { "score": { "$meta": "textScore" } } },
{ "$sort": { "score": 1 } }
])


The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can use $meta aggregation with the $sort stage. Something like



    db.collection.aggregate([
    { "$match": { "$text": { "$search": "red Dangerous" } } },
    { "$addFields": { "score": { "$meta": "textScore" } } },
    { "$sort": { "score": 1 } }
    ])


    The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.






    share|improve this answer

























      up vote
      0
      down vote













      You can use $meta aggregation with the $sort stage. Something like



      db.collection.aggregate([
      { "$match": { "$text": { "$search": "red Dangerous" } } },
      { "$addFields": { "score": { "$meta": "textScore" } } },
      { "$sort": { "score": 1 } }
      ])


      The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can use $meta aggregation with the $sort stage. Something like



        db.collection.aggregate([
        { "$match": { "$text": { "$search": "red Dangerous" } } },
        { "$addFields": { "score": { "$meta": "textScore" } } },
        { "$sort": { "score": 1 } }
        ])


        The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.






        share|improve this answer












        You can use $meta aggregation with the $sort stage. Something like



        db.collection.aggregate([
        { "$match": { "$text": { "$search": "red Dangerous" } } },
        { "$addFields": { "score": { "$meta": "textScore" } } },
        { "$sort": { "score": 1 } }
        ])


        The text score signifies how well the document matched the search term or terms. So the best matched documents will always come on the top.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 16:52









        Anthony Winzlet

        12.1k41038




        12.1k41038















            Popular posts from this blog

            Coverage of Google Street View

            Full-time equivalent

            Surfing