Update an internal document and a field in Mongodb - mongodb

I'm wondering if it's not possible to use update on two elements in the same query. I have this...
db.types.update({"user_id": "aaaawa", "type": "type d"}, {"$set": {"days.7/21/18": {"check": .955}}, {"last_date": new ISODate("2018-07-22)}})
which returns an invalid property id. But if I do them as separate updates they work...updating internal document...
db.types.update({"user_id": "aaaawa", "type": "type d"}, {"$set": {"days.7/21/18": {"check": .955}}})
updating field...
db.types.update({"user_id": "aaaawa", "type": "type d"}, {"$set": {"last_date": new ISODate("2018-07-22)}})

Related

atlas search: How to index UUID field and query by UUID

I have mapped UUID column(_id is UUID in mongo) to string and set the anylyzer to keyword like this in atlas search index mapping
"_id": {
"analyzer": "lucene.keyword",
"type": "string"
},
and when I am querying _id, that's not working.
"text": {
"path": "_id",
"query": "568ae8be-168d-4675-a442-fe93836e1b50"
}
So not sure how UUID mapping should be set up and searched in altas search
This is not supported yet according to
https://www.mongodb.com/community/forums/t/atlas-search-compound-index-uuid-text/142813
https://feedback.mongodb.com/forums/924868-atlas-search/suggestions/41287666-add-support-for-uuid-datatypes-in-atlas-search?_ga=2.54212203.255378698.1643462300-46448687.1607590524&_gac=1.216671778.1643266205.EAIaIQobChMI186v1KvR9QIVkA4rCh3fLwzVEAAYASABEgJtrfD_BwE

Sorting by nested objects attributes in mongoose when using populate

I'm trying to sort parent documents by an attribute from a populated child document.
so if a person schema has an attribute business and the business schema has an attribute name, I want to sort the list of person documents by the name of their business alphabetically.
This seems very possible since the relationship between the person and business is always 1 to 1. but it seems mongoose doesn't allow such sorting mechanism as whenever I pass business.name as sorting arguments it will default sort instead (same sorting as passing unknown arguments).
I'm trying to use aggregate but the docs on that are very bad and not all arguments are clear.
I would like to know if there is a way of doing.
This is my aggregate code:
let populatedArray = [
{
"path": "business",
"schema": require("../models/business").collection.name
},
{
"path": "createdBy",
"schema": require("../models/User").collection.name
},
{
"path": "schema2",
"schema": require("../models/schema2").collection.name
},
{
"path": "schema3",
"schema": require("../models/schema3").collection.name
},
{
"path": "schema4",
"schema": require("../models/schema4").collection.name
},
{
"path": "schema5",
"schema": require("../models/schema5").collection.name
},
{
"path": "schema6",
"schema": require("../models/schema6").collection.name,
"populate": [{
"path": "schema7",
"schema": require("../models/schema7").collection.name
}]
}];
populatedArray.forEach((elem)=>{
docsPromise.lookup({from:elem.schema,localField: elem.path, foreignField: '_id', as: elem.path})
docsPromise.unwind("$"+elem.path)
})
With the unwind command I get no documents. without the unwind command I get 500 documents while I only have 140 in the database. I know that aggregate is near a LEFT_JOIN on a SQL DB which can give similar result but I don't know to stop it form doing so.

select mongodb aray element in a single field in pentaho PDI

Following is the structure of the document i have in a collection in MongoDB
{
"_id": {
"$oid": "5f48e358d43721376c397f53"
},
"heading": "this is heading",
"tags": ["tag1","tag2","tag3"],
"categories": ["projA", "projectA2"],
"content": ["This", "is", "the", "content", "of", "the", "document"],
"timestamp": 1598612312.506219,
"lang": "en"
}
When i am importing data in PDI using the mongodb input step the system is putting each array of the "content" element in a different field
I want to select each element in one field (concat the array type elements). for example in the attached image
above i want one content field with all the arrays concatenated
How do i do that?

Generate a JSON schema from an existing MongoDB collection

I have a MongoDB collection that contains a lot of documents. They are all roughly in the same format, though some of them are missing some properties while others are missing other properties. So for example:
[
{
"_id": "SKU14221",
"title": "Some Product",
"description": "Product Description",
"salesPrice": 19.99,
"specialPrice": 17.99,
"marketPrice": 22.99,
"puchasePrice": 12,
"currency": "USD",
"color": "red",
},
{
"_id": "SKU14222",
"title": "Another Product",
"description": "Product Description",
"salesPrice": 29.99,
"currency": "USD",
"size": "40",
}
]
I would like to automatically generate a schema from the collection. Ideally it would not which properties are present in all the documents and mark those as required. Detecting unique columns would also be nice, though not really all that necessary. In any event I would be modifying the schema after it's automatically generated.
I've noticed that there are tools that can do this for JSON. But short of downloading the entire collection as JSON, is it possible to do this using the MongoDb console or a CLI tool directly from the collection?
You could try this tool out. It appears to do exactly what you want.
Extract (and visualize) schema from Mongo database, including foreign
keys. Output is simple json file or html with dagre/d3.js diagram
(depending on command line options).
https://www.npmjs.com/package/extract-mongo-schema

How to get documents from MongoDB based on greater or less than the given date

I need to get the record from MongoDB based on the date using MongoDB. I am providing my collection below.
f_task:
{
"_id": "5a13731f9402cc17f81ade10",
"taskname": "task1",
"description": "description",
"timestamp": "2017-11-21 05:58:14",
"created_by": "subhra",
"taskid": "858fca9e2e153a61515c0372e079c521",
"created_date": "21-11-2017"
}
Here I need to fetch record as per created_date. Suppose user input is 20-11-2017 or 22-11-2017 then I need query to get the record if the given date is greater than or less than the "created_date" value.