Search and Get count for tree structured matched object in mongodb - database

I am new to backend. I am using golang with mongodb. I have collection whose an array named meta is saved as tree structure. I want get count if objectid for which I am searching matches the id inside the meta array of children. I have attached a screenshot for the understanding. I am unable to write query. I want to compare the id with every moduleid inside the meta and child array for all documents and if matches it should return the count of matched results.
I am trying to query mongodb and unable to find that how I can iterate and compare id at every child node. I have found a soloution to apply aggregation pipeline but I am unable to understand this concept.

Related

How to count number of element in array data in Firestore?

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.

Mongo DB Query to check if document array field element present in more than one document

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}}
])

Skip and Limit on nested array element

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/

ArangoDB Query Attribute Embedded in List

I have a document embedded in a list, and I need to query for documents matching the value of a series of Strings.
My document:
As you can see, I have a regular document. Inside that document is "categories" which is of an unknown length per document. Inside categories I have "confidence" and "title". I need to query to find documents which have a titles matching a list of title I have in an ArrayList. The query I thought would work is:
FOR document IN documents FILTER document.categories.title IN #categories RETURN article
#categories is an ArrayList with a list of titles. If any of the titles in the ArrayList are in the document, I would like it to be returned.
This query seems to be returning null. I don't think it is getting down to the level of comparing the ArrayList to the "title" field in my document. I know I can access the "categories" list using [#] but I don't know how to search for the "title"s in "categories".
The query
FOR document IN documents
FILTER document.categories.title IN #categories
RETURN article
would work if document.categories is a scalar, but it will not work if document.categories is an array. The reason is that the array on the left-hand side of the IN operator will not be auto-expanded.
To find the documents the query could be rewritten as follows:
FOR document IN documents
FILTER document.categories[*].title IN #categories
RETURN document
or
FOR document IN documents
LET titles = (
FOR category IN document.categories
RETURN category.title
)
FILTER titles IN #categories
RETURN document
Apart from that, article will be unknown unless there is a collection named article. It should probably read document or document.article.

MongoDB - get all documents with a property that is saved in a list in a document of another collection

I have documents in a collection that have an array of properties (1-400 different numeric values).
Now i want to get all documents of another collection that have one of these properties (these documents only have one property).
How can i do that, preferably in one call?
As MongoDB is no relational DBMS this isn't possible to achieve with only one call.
What you need to do is to first retrieve your document your want to use for your search. Upon you retrieved it, you're using that array stored in the document todo a $in query on the field for the other collection. So for the mongo shell this could be something like this:
var ar = db.coll1.findOne().numArray
db.coll2.find({b: { $in : ar }})

Resources