mongodb query to get time stamp - mongodb

currently I am learning mongodb.
I have a collection called listings and I want to search a "text" from description field. I want my query to show only text not all the information in the document.
I have created a text index for description field with following command
db.listings.ensureIndex( { description: "text" } )
when I search string "marketing" I am getting all the information in the document. but want to get just marketing word and time stamp with that for analytical purpose. I can count how many time it is been used but how can I know time stamp with that word "marketing"
db.listings.find( { $text: { $search: "marketing" } } ).count()

Try
db.listings.find({$text: {$search: "marketting"}}, {text:1, timestamp: 1}).pretty()

Related

Fetching Data from MongoDB according createdAt column only with date

I have collection Ticket_master,which like as follows
Ticket_master
{
_id:5e70ed53de0f7507d4da8dc2,
"TICKET_NO" : 1000,
"SUBJECT" : "Sub1",
"DESCRIPTION" : "Desc1",
"createdAt" : ISODate("2020-03-17T15:31:31.039Z"),
}
Already tried with the following code snippet and it returns null result set.
db.getCollection('ticket_masters').find({ 'createdAt':ISODate('2020-03-17T00:00:00.000Z')})
How fetch fetch data from ticket_master collection by matching the value with date of creation column,which is represented as createdAt.
Following code snippets return the results. Because it contain both date and time.How fetch data from the collection only with Date .
db.getCollection('ticket_masters').find({ 'createdAt':ISODate('2020-03-17T15:31:31.039+00:00')})
as long as you are searching for all the tickets that have been created on some day, so you can use a date range to filter the tickets
db.getCollection('ticket_masters').find({
createdAt : {
$gte: ISODate('2020-03-17T00:00:00.000Z'),
$lt: ISODate('2020-03-18T00:00:00.000Z')
}
})
this should return all the tickets that have been created on 17/03/2020
check this Mongo Playground

how to write mongoldb query for search/filter

I want to filter input text from database and show that data.as shown in below I have tried these query $text query giving this working for text values but how to filter query for numeric fields like id or date.
find({$text :{$search:<input text>, $caseSensitive: false}})
I want output data which match with input text/value(which can be text, date,number).
MongoDB provides text indexes to support text search queries on string content. text indexes can include any field whose value is a string or an array of string elements.
To perform text search queries, you must have a text index on your collection
db.stores.createIndex( { name: "text", description: "text" } )
You can use your search string with $search parameter :
db.stores.find( { $text: { $search: "coffee",$caseSensitive: false, } } )
Full documentation is available here : https://docs.mongodb.com/manual/text-search/

How to find all the documents that contains “Amazon” in any field as a value? MongoDB

how can I find all the documents that contains “Amazon” in any field as a value in MongoDB?.
Also, is it possible to search for the field and not the value? I heard you could do that but I am not sure how.
Thanks for the help.
You can use text index created on all string fields. You can create such index by using wildcard as below.
db.collection.createIndex( { "$**": "text" } )
In order to search the desired string, you can use
db.collection.find( { $text: { $search: "Amazon" } } )

MongoDb Indexing full text search

I have a document named "posts", it is like this :
{
"_id" : ObjectId("5afc22290c06a67f081fa463"),
"title" : "Cool",
"description" : "this is amazing"
}
And I have putted index on title and description :
db.posts.createIndex( { title: "text", description: "text" } )
The problem is when I search and type for example "amaz" it return the data with "this is amazing" above, while it should return data only when I type "amazing"
db.posts.find({ $text: { $search: 'amaz' } }, (err, results) => {
return res.json(results);
});
Credit to #amenadiel for the original data here:
https://stackoverflow.com/a/24316510/7948962
From the MongoDB docs:
https://docs.mongodb.com/manual/core/index-text/
Index Entries
text index tokenizes and stems the terms in the indexed fields for the index entries. text index stores one index entry for each unique stemmed term in each indexed field for each document in the collection. The index uses simple language-specific suffix stemming.
This is to allow you to search partial "stem" terms in the index, and have the database return all related results. In your specific scenario, amaz is a bit of an odd token as it is a bit irregular compared to other words such as talking, which is tokenized to the word talk, or talked to talk. Similarly walking and walked to walk.
In your case, the word amazing in your text will be tokenized as amaz. If your column contained data such as amazed, it would receive the same amaz token as well. And those results would also be returned from a search of amaz.

How to improve wild card query performance in MongoDB

If I have a collection of 20 million records with a name field. And I want to run a query like db.collection.find({"name": /bob/}) to find all names that contain the word "bob". I know that an index will not help with this query. I am just wondering if there are other ways to improve the query speed? Thanks!
You could add a $text index on the name column:
db.collection.createIndex( { name: "text" } )
and do a search for bob in the name column using this query:
db.collection.find( { $text: { $search: "bob" } } )