I mistakenly created a variable as an array and would like to remove the first element of the array and change the array type to simple strings.
I have been playing around with changing types in mongo from integer to strings and using the $set to change strings to arrays, however I am unable to find a solution to this variation.
The following is the a piece of the document:
db.test.find({"ids":"XXX"}, {"accountability":1}).pretty()
{
"_id" : ObjectId("524c26006f73207e0d977522"),
"accountability" : [
{
"environment" : null,
"performance" : null,
}
]
}
However when I tried to index it, the following appears:
db.test.find({"ids":"XXX"}, {"accountability.performance":1}).pretty()
{
"_id" : ObjectId("524c26006f73207e0d977522"),
"institution" : {
"accountability" : [
{
"performance" : null
}
]
}
}
db.test.find({"ids":"XXX"}, {"institution.accountability.0":1}).pretty()
{
"_id" : ObjectId("52435f0f6f73205f7d37a2b0"),
"accountability" : {
}
}
And lastly:
db.test.find({"ids":"XXX"}, {"accountability.0.performance":1}).pretty()
{
"_id" : ObjectId("524c26006f73207e0d977522"),
"accountability" : [
{
}
]
}
I would appreciate any advice!
Thank you!
Related
I've got the following object and want to update
ArrayOne.ArrayTwo.DocumentTypeID from that ID to a different ID:
{
"_id" : NUUID("97cd06e9-3449-45db-be69-fce30ee1f9b8"),
"ArrayOne" : [
{
"ID" : NUUID("70d99c86-414c-401a-9cc9-91661806c10f"),
"ArrayTwo" : [
{
"EvidenceID" : NUUID("0799492a-b312-4832-a830-ce4ff841d5fd"),
"DocumentTypeID" : NUUID("fa67acee-2d56-4174-954f-f1bdc892add0"),
"FileID" : NUUID("e5f433c6-8c7f-008d-6d2a-12a908f91690"),
"FileName" : "testqwerty.pdf",
"AdjudicationStatus" : 0,
"SubmitDateTime" : ISODate("2022-09-12T14:14:15.060Z")
}
],
"Propertyone" : null,
"Propertytwo" : null,
"Propertythree" : 0
}
]
}
Trying to use:
db.getCollection('Collection').updateMany
(
{"ArrayOne.ArrayTwo.DocumentTypeID" : NUUID("FA67ACEE-2D56-4174-954F-F1BDC892ADD0")},
{$set : {"ArrayOne.ArrayTwo.DocumentTypeID" : NUUID("090F89EF-3AAE-45F2-853F-5B8C7B8AC0CA")}}
)
I do not want to specify the element as 'ArrayOne' can have up to 10 elements and so can "ArrayTwo" the DocumentTypeID however has a unique GUID.
Can someone please let me know how I can do this ?
Thanks
I have a database of products in MongoDB. Inside main objects, there is another object "GL_related", and inside the "GL_related" object there is an array "also_viewed". How can I count the elements inside the "also_viewed" array of a specific object with the title "Sunglasses"(by using the aggregate pipeline method)?
The result should be "4".
Sample data:
/* 1 */
{
"_id" : ObjectId("5d0b6d1cd7367de7f58b4908"),
"GL_asin" : "0456840532",
"GL_title" : "Sunglasses",
"GL_related" : {
"also_viewed" : [
"B001K9DPPC",
"B00BV9MU2K",
"B0042FHTDK",
"B003KK5LEW"
]
}
}
/* 2 */
{
"_id" : ObjectId("5d0b6d1cd7367de7f58b4907"),
"GL_asin" : "0456856293",
"GL_title" : "T-Shirt",
"GL_related" : {
"also_viewed" : [
"B005P1HF2U",
"B006K5JWOE"
]
}
}
You can just use Mongo's dot notation combined with $size
db.collection.aggregate([
{
"$project": {
itemCount: {
$size: "$GL_related.also_viewed"
}
}
}
])
Mongo Playground
I'm trying to query single object from an array of objects.
The array looks like this.
db.getCollection('user').getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
}
},
{
"v" : 1,
"key" : {
"name" : 1
}
},
{
"v" : 1,
"key" : {
"admin" : 1
}
}
]
Don't know how to find an element with "_id" property name in "key" object.
db.getCollection('user').getIndexes().[some sorting query];
{
"v" : 1,
"key" : {
"_id" : 1
}
}
Does anyone have an idea? Thank you.
The getIndexes helper in the mongo shell accepts a filter as an argument, like:
db.getCollection("user").getIndexes({"key._id":{$exists:true}})
From a driver, you can run the listIndexes command similarly:
database.runCommand({listIndexes:"user", "key._id":{$exists:true}})
I've got an array of Project ID's, for example:
[ 'ExneN3NdwmGPgRj5o', 'hXoRA7moQhqjwtaiY' ]
And in my Questions collection, I've got a field called 'project', which has a string of a project Id. For example:
{
"_id" : "XPRbFupkJPmrmvcin",
"question" : "Vraag 13",
"answer" : "photo",
"project" : "ExneN3NdwmGPgRj5o",
"datetime_from" : ISODate("2017-01-10T08:01:00Z"),
"datetime_till" : ISODate("2017-01-10T19:00:00Z"),
"createdAt" : ISODate("2017-01-10T08:41:39.950Z"),
"notificationSent" : true
}
{
"_id" : "EdFH6bo2xBPht5kYW",
"question" : "sdfadsfasdf",
"answer" : "text",
"project" : "hXoRA7moQhqjwtaiY",
"datetime_from" : ISODate("2017-01-11T11:00:00Z"),
"datetime_till" : ISODate("2017-01-11T17:00:00Z"),
"createdAt" : ISODate("2017-01-10T10:21:42.147Z"),
"notificationSent" : false
}
Now I want to return all documents of the Questions collection, where the Project (id) is one of the value's from the Array.
To test if it's working, I'm first trying to return one document.
Im console.logging like this:
Questions.findOne({project: { $eq: projectArray }})['_id'];
but have also tryed this:
Questions.findOne({project: { $in: [projectArray] }})['_id'];
But keep getting 'undefined'
Please try this.
Questions.find({project: { $in: projectArray }}) => for fetching all docs with those ids
Questions.findOne({project: { $in: projectArray }}) => if you want just one doc
I have simple mapping - one string field and one string[] field.
The array of strings contains duplicate values, and I get those duplicate values in query:
{ "query" : { "term" : {"id" : "579a252585b8c5c428fa0a3c"} } }
Returns a single valid hit:
{
"id" : "579a252585b8c5c428fa0a3c",
"touches" : [ "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b457b", "5639abfb5cba47087e8b457b"
}
But in metric script aggregation:
"aggs": {
"path": {
"scripted_metric": {
"map_script": "_agg['result'] = doc['touches'].values"
}
}
}
retuns
"aggregations" : {
"path" : {
"value" : [ { }, {
"result" : [ "5639abfb5cba47087e8b4571", "5639abfb5cba47087e8b457b" ]
}, { }, { }, { } ]
}
}
that element is org.elasticsearch.index.fielddata.ScriptDocValues$Strings, casting it toString() returns a json-encoded 2-element array.
So, the question:
Why does ScriptDocValues$Strings return only unique array values and how to get the initial array in script aggregation?
Thanks.
UPD
I found that for numerical values (in particular floats) everything works perfect.