Jak odpytywać MongoDB za pomocą "Lubię to"?
Chcę coś odpytywać jako zapytanie SQL like
:
select *
from users
where name like '%m%'
Jak zrobić to samo w MongoDB?
Nie mogę znaleźć operatora like
w .
30 answers
To musiałoby być:
db.users.find({"name": /.*m.*/})
Lub podobne:
db.users.find({"name": /m/})
Szukasz czegoś, co zawiera gdzieś "m" (operator SQL '%
' jest odpowiednikiem wyrażenia regularnego '.*
'), a nie czegoś, co ma " m " zakotwiczone na początku łańcucha.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-14 04:07:37
db.users.insert({name: 'paulo'})
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})
db.users.find({name: /a/}) //like '%a%'
Out: paulo, patric
db.users.find({name: /^pa/}) //like 'pa%'
Out: paulo, patric
db.users.find({name: /ro$/}) //like '%ro'
Out: pedro
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-28 18:53:40
W
- PyMongo using Python
- Mangusta za pomocą węzła.js
- Jongo , using Java
- mgo , używając Go
Możesz zrobić:
db.users.find({'name': {'$regex': 'sometext'}})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-27 13:17:41
W PHP możesz użyć następującego kodu:
$collection->find(array('name'=> array('$regex' => 'm'));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-10 10:04:26
Użyłbyś do tego regex w mongo.
Np: db.users.find({"name": /^m/})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-22 03:48:56
Jeśli używasz node.js, to mówi, że możesz to napisać:
db.collection.find( { field: /acme.*corp/i } );
//or
db.collection.find( { field: { $regex: 'acme.*corp', $options: 'i' } } );
Również , możesz napisać to:
db.collection.find( { field: new RegExp('acme.*corp', 'i') } );
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-21 07:44:03
Jest już wiele odpowiedzi. Podaję różne typy wymagań i rozwiązań dla wyszukiwania ciągów z wyrażeniami regularnymi.
Można zrobić z regex, które zawierają słowo lub.e like. Możesz również użyć $options => i
do wyszukiwania bez rozróżniania wielkości liter
Zawiera string
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
Nie zawiera string
tylko z regex
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
Dokładny rozmiar string
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
Zacznij od string
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
End with string
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
Zachowaj to jako zakładkę i odniesienie do wszelkich innych zmian, które mogą być potrzebne.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-11 15:39:09
Masz 2 możliwości:
db.users.find({"name": /string/})
Lub
db.users.find({"name": {"$regex": "string", "$options": "i"}})
Na drugim masz więcej opcji, takich jak " i " w opcjach, aby znaleźć używając rozróżniania wielkości liter. A co do "string", można użyć jak".string. "(%string%), lub " string.* "(string%) i ".* string) (%string) na przykład. Możesz używać wyrażenia regularnego, jak chcesz.
Smacznego!Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-25 21:58:19
Already u got the answers but to match regex with case insensitivity
Możesz użyć następującego zapytania
db.users.find ({ "name" : /m/i } ).pretty()
i
w /m/i
wskazuje niewrażliwość na wielkość liter i .pretty()
zapewnia ładniejsze wyjście
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-23 11:21:39
Dla mangusty w węźle.js
db.users.find({'name': {'$regex': '.*sometext.*'}})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-10 11:39:29
Możesz użyć nowej funkcji 2.6 mongodb:
db.foo.insert({desc: "This is a string with text"});
db.foo.insert({desc:"This is a another string with Text"});
db.foo.ensureIndex({"desc":"text"});
db.foo.find({
$text:{
$search:"text"
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-04 19:19:03
Dla PHP mongo Like.
Miałem kilka problemów z php mongo jak. odkryłem, że konkatenacja regex params pomaga w niektórych sytuacjach PHP mongo find field zaczyna się od. Pomyślałem, że napiszę tutaj, aby przyczynić się do bardziej popularnego wątku
E. g
db()->users->insert(['name' => 'john']);
db()->users->insert(['name' => 'joe']);
db()->users->insert(['name' => 'jason']);
// starts with
$like_var = 'jo';
$prefix = '/^';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
output: (joe, john)
// contains
$like_var = 'j';
$prefix = '/';
$suffix = '/';
$name = $prefix . $like_var . $suffix;
db()->users->find(['name' => array('$regex'=>new MongoRegex($name))]);
output: (joe, john, jason)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:14
In nodejs project and use mongoose use Like query
var User = mongoose.model('User');
var searchQuery={};
searchQuery.email = req.query.email;
searchQuery.name = {$regex: req.query.name, $options: 'i'};
User.find(searchQuery, function(error, user) {
if(error || user === null) {
return res.status(500).send(error);
}
return res.status(200).send(user);
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-10 18:57:51
Możesz użyć instrukcji where do zbudowania dowolnego skryptu JS:
db.myCollection.find( { $where: "this.name.toLowerCase().indexOf('m') >= 0" } );
Odniesienie: http://docs.mongodb.org/manual/reference/operator/where/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-05 14:53:27
W SQL zapytanie 'like ' wygląda tak:
select * from users where name like '%m%'
W konsoli MongoDB wygląda to tak:
db.users.find({"name": /m/}) // Not JSON formatted
db.users.find({"name": /m/}).pretty() // JSON formatted
W addion pretty()
metoda we wszystkich miejscach wytworzy sformatowaną strukturę JSON, która jest bardziej czytelna.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-19 11:15:12
Użyj wyrażeń regularnych pasujących jak poniżej. "I" pokazuje niewrażliwość na przypadki.
var collections = mongoDatabase.GetCollection("Abcd");
var queryA = Query.And(
Query.Matches("strName", new BsonRegularExpression("ABCD", "i")),
Query.Matches("strVal", new BsonRegularExpression("4121", "i")));
var queryB = Query.Or(
Query.Matches("strName", new BsonRegularExpression("ABCD","i")),
Query.Matches("strVal", new BsonRegularExpression("33156", "i")));
var getA = collections.Find(queryA);
var getB = collections.Find(queryB);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-16 11:20:25
W Go i sterowniku mgo:
Collection.Find(bson.M{"name": bson.RegEx{"m", ""}}).All(&result)
Gdzie wynikiem jest instancja struct poszukiwanego typu
Jak zapytanie będzie jak pokazano poniżej
db.movies.find({title: /.*Twelve Monkeys.*/}).sort({regularizedCorRelation : 1}).limit(10);
Dla scala ReactiveMongo api,
val query = BSONDocument("title" -> BSONRegex(".*"+name+".*", "")) //like
val sortQ = BSONDocument("regularizedCorRelation" -> BSONInteger(1))
val cursor = collection.find(query).sort(sortQ).options(QueryOpts().batchSize(10)).cursor[BSONDocument]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-18 09:55:15
W MongoDB Compass musisz użyć składni strict mode, jako takiej:
{ "text": { "$regex": "^Foo.*", "$options": "i" } }
(w MongoDB Compass ważne jest, aby używać "
zamiast '
)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-19 08:01:35
Jeśli używasz Spring-Data Mongodb, możesz to zrobić w następujący sposób:
String tagName = "m";
Query query = new Query();
query.limit(10);
query.addCriteria(Criteria.where("tagName").regex(tagName));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-28 10:34:23
Jako regex obsługi powłoki Mongo, jest to całkowicie możliwe.
db.users.findOne({"name" : /.*sometext.*/});
Jeśli chcemy, aby zapytanie było niewrażliwe na wielkość liter, możemy użyć opcji "i", jak pokazano poniżej:
db.users.findOne({"name" : /.*sometext.*/i});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-19 08:09:23
Jeśli chcesz wyszukać "Lubię to" w mongo, powinieneś użyć $regex używając tego zapytania będzie
db.product.find({name:{$regex:/m/i}})
Więcej informacji można znaleźć w dokumentacji. https://docs.mongodb.com/manual/reference/operator/query/regex/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-23 15:03:51
Wydaje się, że istnieją powody do używania zarówno wzorca javascript /regex_pattern/
, jak i wzorca mongo {'$regex': 'regex_pattern'}
. Zobacz: MongoBD Ograniczenia Składni RegEx
Nie jest to kompletny samouczek RegEx, ale zainspirowałem się do uruchomienia tych testów po obejrzeniu wysoko głosowanego dwuznacznego postu powyżej .
> ['abbbb','bbabb','bbbba'].forEach(function(v){db.test_collection.insert({val: v})})
> db.test_collection.find({val: /a/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }
> db.test_collection.find({val: /.*a.*/})
{ "val" : "abbbb" }
{ "val" : "bbabb" }
{ "val" : "bbbba" }
> db.test_collection.find({val: /.+a.+/})
{ "val" : "bbabb" }
> db.test_collection.find({val: /^a/})
{ "val" : "abbbb" }
> db.test_collection.find({val: /a$/})
{ "val" : "bbbba" }
> db.test_collection.find({val: {'$regex': 'a$'}})
{ "val" : "bbbba" }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:53
Znalazłem darmowe narzędzie do tłumaczenia zapytań MYSQL na MongoDB. http://www.querymongo.com / Sprawdziłem kilka zapytań. jak widzę prawie wszystkie są poprawne. Zgodnie z tym, odpowiedź brzmi
db.users.find({
"name": "%m%"
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-06 05:07:03
Regex są drogie są Proces.
Innym sposobem jest utworzenie indeksu tekstu, a następnie przeszukiwanie go za pomocą $search
.
Utwórz indeks tekstowy pól, które chcesz przeszukiwać:
db.collection.createIndex({name: 'text', otherField: 'text'});
Wyszukiwanie ciągu w indeksie tekstowym:
db.collection.find({
'$text'=>{'$search': "The string"}
})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-16 05:59:08
MongoRegex został wycofany.
Use MongoDB\BSON \ Regex
$regex = new MongoDB\BSON\Regex ( '^m');
$cursor = $collection->find(array('users' => $regex));
//iterate through the cursor
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-27 20:06:51
Jeśli używasz PHP, możesz użyć wrappera MongoDB_DataObject Jak poniżej:
$model = new MongoDB_DataObject();
$model->query("select * from users where name like '%m%'");
while($model->fetch()) {
var_dump($model);
}
Lub:
$model = new MongoDB_DataObject('users);
$model->whereAdd("name like '%m%'");
$model->find();
while($model->fetch()) {
var_dump($model);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-16 08:34:08
FullName like 'last' with status = = 'Pending' between two dates:
db.orders.find({
createdAt:{$gt:ISODate("2017-04-25T10:08:16.111Z"),
$lt:ISODate("2017-05-05T10:08:16.111Z")},
status:"Pending",
fullName:/last/}).pretty();
Status = = "oczekujący" i order LIKE "PHA876174':
db.orders.find({
status:"Pending",
orderId:/PHA876174/
}).pretty();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-06 08:38:57
db.customer.find({"customerid": {"$regex": "CU_00000*", "$options": "i"}}).pretty()
Kiedy szukamy wzorców łańcuchów, zawsze lepiej jest użyć powyższego wzorca, tak jak wtedy, gdy nie jesteśmy pewni co do wielkości liter. Mam nadzieję, że to pomoże!!!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-02 06:06:13
Użyj wyszukiwania podciągów agregacji (z indeksem!!!):
db.collection.aggregate([{
$project : {
fieldExists : {
$indexOfBytes : ['$field', 'string']
}
}
}, {
$match : {
fieldExists : {
$gt : -1
}
}
}, {
$limit : 5
}
]);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-03 19:16:55