I will filter by the director field, and get all the documents that end with ‘Spielberg’:ĭb.getCollection("movies"). Too bad that in spite of being my favourite director, I can only remember his last name: oh well, that will have to do. ![]() ![]() Now I am going to get a list of movies of my favourite director. As an example let´s imagine we got a MongoDB collection of movies that has the following fields: title, plot, and director. In MongoDB we can query string fields quite easily using regular expressions. To import or export data to or from a collection, navigate to the detailed collection view by either selecting the collection from the Databases tab or clicking the collection in. Compass supports import and export for both JSON and CSV files. Now let´s see how we can achieve the same with MongoDB. You can use MongoDB Compass to import and export data to and from collections. SELECT * FROM names n where n.name LIKE '%a%' This query will find all values that contains a ‘a’ in any position: SELECT * FROM names n where n.name LIKE '%Smith' This query will get all the values ending with ‘Smith’: ![]() SELECT * FROM names n where n.name LIKE 'James%' Developers who had worked with SQL Server are well familiar with the % wildcard that represents 0 or any number of characters.įor example, this query will find all values starting with ‘James’: SELECT * FROM names n where n.name LIKE 'James Smith'īy combining the use of the LIKE function with wildcards, we can find all values that start, end, or contain a string at a given position. LIKE allow us to check if 2 strings are equal: In SQL we commonly use the LIKE function to compare strings. In this post we saw the basics of MongoDB filtering, now it is time to have a closer look at how to filter and search information in MongoDB collections when dealing with attributes of type string.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |