In Mongo, how do I find all the documents, but display only their titles? - mongodb

There's a ['title'] attribute inside each document.
How can I do a .find, but only display the title?

You can retrieve a subset of fields within the documents as follows:
e.g. looking retrieving only the title field for books with a given ISBN:
db.Books.find({ISBN : "1234567890" }, { title : 1 } );
You can also do field negation, i.e. return all fields EXCEPT the ones you specify. There's a good reference on this on MongoDB.org

you can find that info at http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

Related

How to nested query in mongodb?

How to index nested objects in Pymongo so that I can perform full text search. For example I've the collection object like this...
{
"_id":"ObjectId(" "5e8b0fa1c869790699efdb2d" ")",
"xmlfileid": "334355343343223567",
"threads":{
"threads_participants":{
"participant":[
{
"#reference": rits_dbx_1
},
{
"#reference": rits_dbx_2
}
]
},
"thread":{
"namedAnchor":"{' ': 'NORP', 'Ho': 'PERSON', 'Lets': 'PERSON', 'Boris Johnson': 'PERSON', 'Britain': 'GPE'}",
"selectedText":{
"fragment":[
{
"#class":"next_steps",
"#text":"rits_dbx_1 said hello this is a good site."
},
{
"#class":"other",
"#text":"rits_dbx_1 said ho ho."
},
{
"#class":"other",
"#text":"rits_dbx_1 said lets put some meaningful stuff here."
},
]
}
}
}
}
I've placed search box in my website and when user types the #text in search box I want to display the #text and class and the xmlfileid
So far I've created index using below command. And I don't know it's the right way to get the result and also please help with query too.
db.xml_collection.createIndex({"threads.thread.selectedText.fragment": "text"})
In my python code I've this but that prints nothing
result = collection.find({"$text": {"$search": "ho ho"}})
Your index is wrong.
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.
https://docs.mongodb.com/manual/core/index-text/
If you want to index only #text field, change your index to this:
db.xml_collection.createIndex({"threads.thread.selectedText.fragment.#text": "text"})
Also, you may create Wildcard text index and MongoDB will index all key:value pairs (where value is string / array of string)
db.xml_collection.createIndex({"$**": "text"})
Note: You need to drop any previous text indexes for this collection

MongoDB - Querying a field in a lower level of document

Issue with fetching the data of a particular field within a document
The followoing is my collection structure
colleciton mail
mailtype
criteria
triggerinfor
code ----> multiple codes are there.
description
status
Tried this but not working
db.getCollection('Mail').find(
{
"MailType" :"Printed",
"MailName" :"Welcomemail",
"programType":"Maile",
"criterias" :
{"triggerInformation":
{"code" :"MAIL007"},
{"description":"1234 Welcome maile"}
}
}
)
I have to retrieve the document with code MAIL007 only.. could some one help in getting a query for that
There are two ways to do it, depending on what you need:
1- Matching an exact nested document: this way, you are querying for documents that have a nested document that is exactly like the one in your search criteria.
db.getCollection('Mail').find(
{
"MailType" :"Printed",
"MailName" :"Welcomemail",
"programType":"Maile",
"criterias.triggerInformation": {
"code": "MAIL007",
"description": "1234 Welcome maile",
"status": "some status"
}
}
Notice that you need to explicitly inform a value to each of the nested document fields in your search criteria. If any field is missing the query will return nothing.
2- Querying a field inside a nested document: this way, you are querying for documents based on the value of one or more fields inside a nested document.
db.getCollection('Mail').find({
"MailType":"Printed",
"MailName":"Welcomemail",
"programType":"Maile",
$and: [
{"criterias.triggerInformation.code": "MAIL007"},
{"criterias.triggerInformation.description": "description":"1234 Welcome maile"}
]
})
In the code above you are querying for documents based on the values of the fields code and description inside the nested document criterias.triggerInformation.
You can find more info about querying nested documents at MongoDB's docs here

Mongodb query variable number of search terms

I am trying to design a query based on documents containing an array of metadata.
book = {
title : String,
metaData : [String]
}
To find the desired books I have another search string array containing multiple metadata terms. The length of the array can be variable in the number of metadata search terms present. How can I query to find only books that contain all the specified metadata search terms?
Example:
book1 - nature, trees, insects, fog, music
book2 - music, art, sports
Search using metadata of [music, sports] would yield book2.
How can I most efficiently design this query? Can I do this and avoid a nested query? Any help would be greatly appreciated.
You can do this by using the $all operator.
From the docs:
If, instead, you wish to find an array that contains both the elements "red" and "blank", without regard to order or other elements in the array, use the $all operator:
db.inventory.find( { tags: { $all: ["red", "blank"] } } )
MongoDB CRUD Operations: Query an array
You can use $all to get all the documents containing the given metadata strings.
Try:
let given_metadata_array = ["music", "sports"];
db.book.find({
metadata : {$all : given_metadata_array}
})
Read more about $all official documentation for detailed information.

Get text words from query

I've read the MongoDB documentation on getting the indexes within a collection, and have also searched SO and Google for my question. I want to get the actual indexed values.
Or maybe my understanding of how MongoDB indexes is incorrect. If I've been indexing a field called text that contains paragraphs, am I right in thinking that what gets indexed is each word in the paragraph?
Either case I want to retrieve the values that were indexed, which db.collection.getIndexes() doesn't seem to be returning.
Well yes and no, in summary.
Indexes work on the "values" of the fields they are supplied to index, and are much like a "card index" in that there is a point of reference to look at to find the location of something that matches that term.
What "you" seem to be asking about here is "text indexes". This is a special index format in MongoDB and other databases as well that looks at the "text" content of a field and breaks down every "word" in that content into a value in that "index".
Typically we do:
db.collection.createIndex({ "text": "text" })
Where the "field name" here is "text" as you asked, but more importantly the type of index here is "text".
This allows you to then insert data like this:
db.collection.insert({ "text": "The quick brown fox jumped over the lazy dog" })
And then search like this, using the $text operator:
db.collection.find({ "$text": { "$search": "brown fox" } })
Which will return and "rank" in order the terms you gave in your query depending how they matched the given "text" of your field in the index on your collection.
Note that a "text" index and it's query does not interact on a specific field. But the index itself can be made over multiple fields. The query and the constraints on the "index" itself are that there can "only be one" text index present on any given collection otherwise errors will occur.
As per mongodb's docs:
"db.collection.getIndexes() returns an array of documents that hold index information for the collection. Index information includes the keys and options used to create the index. For information on the keys and index options, see db.collection.createIndex()."
You first have to create the index on the collection, using the createIndex() method:
db.records.createIndex( { userid: 1 } )
Queries on the userid field are supported by the index:
Example:
db.records.find( { userid: 2 } )
db.records.find( { userid: { $gt: 10 } } )
Indexes help you avoid scanning the whole document. They basically are references or pointers to specific parts of your collection.
The docs explain it better:
http://docs.mongodb.org/manual/tutorial/create-an-index/

How to find all the comment of a single author for the given data-set in mongodb?

Let's say I have a blog dataset which looks like ths
{
"_id" : "abcde",
"author" : "xyz",
"body" : "this is test body",
"date" : "xyz",
"tags" : ["tag1","tag2"],
"comments":[
{ "body": "comment1",
"email": "email1",
"author" :"author1"
},
{ "body": "comment2",
"email": "email2",
"author" :"author2"
}
]
}
Here each document is representing a blog post. Each blog post can have multiple comments, but one my one user. Let's say I have to find all comments made by a particular author. What will be the query for that?
One approach would be to unwind comments, then group by comments.author and push comments.body.
However, Can it be done without aggregation pipeline, just by using find? Because I think I am supposed to do it just by find. Can anyone help me with this?
You can use dot notation to query against an array of subdocuments.
Your query would look something like this:
db.blog_posts.find({"comments.author": "author1"});
This will return all documents which contain subdocuments that have an author value of author1. The results will be the entire post document, so you might want to specify the fields that you want returned. This is called query projection.
db.blog_posts.find({"comments.author": "author1"});
To specify what fields you want, add an extra object as the second parameter to your .find() function:
db.blog_posts.find({"comments.author": "author1"}, {"comments": 1} );
Now the resulting documents will only contain the _id field and the comments field.
Keep in mind that you are actually querying the blog posts collection so your returned results will still be the blog posts - but only the ones where the specified author has commented on.
A simple way to take the next step and extract only the comment objects might look something like this:
var author = 'author1';
var comments = [];
db.blog_posts.find(...).forEach(function(doc){
var post_comments = doc.comments;
for (var i=0; i<post_comments.length; i++){
if (post_comments[i].author == author){
comments.push(post_comments[i]);
}
}
});
Here is the relevant documentation page:
http://docs.mongodb.org/manual/tutorial/query-documents/#array-of-embedded-documents
Match a Field Without Specifying Array Index
If you do not know the index position of the document in the array, concatenate the name of the field that contains the array, with a dot (.) and the name of the field in the embedded document.