I am studying Firestore with React.
After I added the array data into the firestore, I am trying to count the certain element in array of data in the document.
Like the picture I attached, there are several documents includes array data.
I want to count the number of CS 34800 in class array from all documents.
What is the best way to get it?
Thank you!
enter image description here
There are no counting or aggregating operations on Firebase, so that way you have it now, you will have to query for all the documents that match your criteria, then count the number of documents in your result set.
You can use an "array_contains" operator to get all the documents where an array contains some value.
collectionRef.where("class", "array-contains", "CS 34800").get()
.then(querySnapshot => {
const count = querySnapshot.size
});
If you need to get a count without making a query for documents, you'll have to maintain that yourself for every string that you could possibly need to count, which is an entirely different problem.
Related
I have been searching through the MongoDB query syntax with various combinations of terms to see if I can find the right syntax for the type of query I want to create.
We have a collection containing documents with an array field. This array field contains ids of items associated with the document.
I want to be able to check if an item has been associated more than once. If it has then more than one document will have the id element present in its array field.
I don't know in advance the id(s) to check for as I don't know which items are associated more than once. I am trying to detect this. It would be comparatively straightforward to query for all documents with a specific value in their array field.
What I need is some query that can return all the documents where one of the elements of its array field is also present in the array field of a different document.
I don't know how to do this. In SQL it might have been possible with subqueries. In Mongo Query Language I don't know how to do this or even if it can be done.
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field in 3.6 mongo version.
$push with [] array non equality match to output document where there is matching document.
db.col.aggregate([
{"$unwind":"$array"},
{"$lookup":{
"from":col,
"localField":"array",
"foreignField":"array",
"as":"jarray"
}},
{"$group":{
"_id":"$_id",
"fieldOne":{"$first":"$fieldOne"},
... other fields
"jarray":{"$push":"$jarray"}
}},
{"$match":{"jarray":{"$ne":[]}}},
{"$project":{"jarray":0}}
])
I'm new to mongodb 3.2 and I'm wondering what the accepted method to store an array.
I have a safe words array that just contains... words.
I want to store it in mongodb for a fast comparison of a queried word.
should I just create a document, and add to it objects with one property called 'word' that contains a word?
should I create a document with one object that contains the property words with an array of words?
maybe something else?
any ideas?
MongoDB supports dynamic schema i.e The documents stored in the database can have varying sets of fields, with different types for each field
Following example demonstrates storing an array in MongoDB document
{
_id : ObjectId("56bc838924f5ca3e0d3c9871"),
"tags":["mongodb","NoSQL"]
}
In above example tags is an array field which stores all tags
I want to apply skip and limit for paging in nested array of a document how can I perform this [Efficient Way]
My Document recored like
{
"_id":"",
"name":"",
"ObjectArray":[{
"url":"",
"value":""
}]
}
I want to retrieve multiple document and every document contain 'n' number of record.
I am using $in in find query to retrieve multiple record on basis of _id but how can i get certain number of element of ObjectArray in every document?
You can try like this -
db.collection.find({}, {ObjectArray:{$slice:[0, 3]}})
This will provide you records from 0..3
$slice:[SKIP_VALUE, LIMIT_VALUE]}
For your example:-
db.collection.find({"_id":""}, {ObjectArray:{$slice:[0, 3]}})
Here is the reference for MongoDB Slice feature.
http://docs.mongodb.org/manual/reference/operator/projection/slice/
I have data in which field have following java data types.
What would be the best way to index such kind of data.
Thanks,
field_a map<string,string>
field b map<string,array<string>>
How to define schema.xml for it
Currently Solr doesn't support map type field type. So, you can not query on some particular key inside the map and retrieve its value. I don't know whether it'll be helpful you or not, but I can suggest you a way to keep this in Solr.
You can store the map in a field as a json formatted string. Say, document1 has map1 in field_a and document2 has map2 in field_a. Now, you keep some distinct data related to each map to their corresponding documents. When you want to query, query on those fields in stead of the maps. Then in the search result, when you retrieve the json formatted string, parse it in your application and get the values.
Hope this will help.
I want to perform a comparison based on one field in a Solr document before it is sent to the Writer or to the user. I want to have the final result object, probably SolrDocumentList, so that I can loop through all the SolrDocument objects and perform a field to field comparison. For instance, if my search returns 10 documents and 5 documents have myfield="myValue", my final list should contain 6 documents with only one document having myfield="myValue", the other 4 documents should be discarded, regardless of what the other fields' contents are.
Is there any plugin for this?
If not, where should I place my code?
You can use Result Grouping / Field Collapsing. Try some thing like this: &q=solr+memory&group=true&group.field=manu_exact&group.main=true
More documenation here: http://wiki.apache.org/solr/FieldCollapsing