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
Related
I am new to mongodb. So I don't know what is the better way to query array field. I have a schema where in our collection of courses we have a program field. In start it was Reference to program collection and contain program id. But Now we have change the schema from Reference to array of Reference, so we can allow course to be a part of multiple program. In our codebase all the query are written like this:
course.find({program});
Do I have to change this query to cater this schema change like this:
course.find({program: {$in: program}});
I have tested in mongodb compass and this query
course.find({program});
Work on array field.
So let me know what could be consequence if i don't use $in operator while search the array field.
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 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 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 }})
does anyone know how to create a custom field in solr 1.4? I need to create a field containing sub values of the same type, say 3 strings.
The problem is abt something like this: suppose i want to declare in the schema an "image" field, which has 3 sub-fields (strings) like "path", "title", "thumb_path".
Any ideas?
I know in solr 1.5 there will be probably the concept of LatLon object, to contain the 2 values -doubles- of latitude and longitude. Have you seen something like that?
Luca
Sounds like you should consider creating an index of "image" documents in your SOLR index.
Each image "doc" would have the fields:
title
path
thumb_path
[gallery]
where [gallery] is a multi-value field (assuming images can appear in more than one gallery)
To construct a gallery page, you run the query "gallery:foo" and then iterate over the list of images, populating the HTML elements from the fields: title, path, etc.
Note: SOLR does not limit you to having a single document type in its index (whether its best practice is another matter). So you could also index other non-image documents as well. In this case its advisable to have a field like "doc_type" so you can limit searches to that type