I am using mongo-parse to parse the mongo queries. Below is my code
var parser = require('mongo-parse')
var queryObject = parser.parse({"city":"Paris"})
//Getting field and value separately
//Field = city
//Value = Paris
var string = 'db.employee.find({"city":"Paris"},{"emp_id":1}'
//Not able to get the field and value separately from string
Is there any node module to generate the mongo query from the above string.
Any help on this will be really helpful
it doesn't look like mongo-parse returns any information about the collection. considering this, couldn't you just take the analysis you get from the parser and hand-construct the query from the data (assuming i'm understanding your question correctly)
e.g.
var newQuery = {};
newQuery[queryObject.field] = queryObject.value;
db.employee.find(newQuery, {emp_id: 1});
Related
I'm using mongoDB, mongoose and typescript and I need to keep the document ids when I query but I can only get the type _id: new ObjectId("62aa4bddae588fb13e8df552") . I only need to keep the string "62aa4bddae588fb13e8df552" to later store it and do other processing. I can't get rid of the new ObjectId
async findById(id:string) {
const convert = {"_id":id}
const userfindById = await userModel.findById(convert);
const iD = userfindById?._id
return userfindById;
}
ObjectId is just a type :
https://www.mongodb.com/docs/manual/reference/bson-types/#std-label-objectid
If you want to get the string you can extract with _id.toString(), if you want to store the string you should change the type of _id (or create a new property)
I need to fetch the list of persons from an array(Ex: String[] getPersons) as input. I can't figure out how to compare and fetch data from Cosmos DB using a LINQ expression. I experimented with GetItemLinqQueryable, but I don't know if it's the right way to use it.
var db = Client.GetDatabase(databaseId);
var container = db.GetContainer(containerId);
var q = container.GetItemLinqQueryable<Person>();
var iterator = q.Where(p => p.Name == getPersons[0]).ToFeedIterator();
var results = await iterator.ReadNextAsync();
If I am using the above one, I could only able to get only the first person result but I need to get the other persons in the array as well.
You can use Contains. It is equivalent to ARRAY_CONTAINS in Cosmos DB Array functions.
You can try this code:
var iterator = q.Where(p => getPersons.Contains(p.Name)).ToFeedIterator();
My goal is really simple, I want to design this particular json result into mongoose data structure.
Json example
{
"question": "Who was the 13th president of the United States?",
"choice_1": "Millard Fillmore",
"choice_2": "Zachary Taylor",
"choice_3": "Franklin Pierce",
"choice_4" :"James K. Polk",
"answer" :"choice_1"
},
My goal is to transform this json design into mongoose design, and my attempt so far.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const QuestionSchema = new Schema({
question: String,
choice_1: String,
choice_2: String,
choice_3: String,
choice_4: String,
answer: String
});
So later on, during the creation of the question, how do I assign one of the choices to the answer attribute?
const Question = require('/question');
const question = new Question();
question.question = req.body.question;
question.choice_1 = req.body.choice_1;
question.choice_2 = req.body.choice_2;
question.choice_3 = req.body.choice_3;
question.choice_4 = req.body.choice_4;
question.answer = // how do i put choice 1 - 3 here?
Your example refers to the answer by its property name, so:
question.answer = 'choice_3';
Later on, after running a query to retrieve a question, you can use the value of .answer to convert it back to the actual text (for display purposes):
let answer = question[question.answer];
My Schema is:
var schema = new Schema({
players: {
type: [Schema.Types.ObjectId]});
I'm using Promises. My simple query to fetch the collection by id is:
MyModel.findById(id).exec()
.then(function (doc) {
//here I'm getting my doc
})
.catch(next)
I'm getting my document but the problem is here I'm getting an array of ObjectIds and I want to compare this array of ObjectIds with an ObjectId which I have in the string form. In one case I want to find the index of this string id in that array of ObjectIds.
I tried by lodash like
var index = _.indexOf(doc.players, idInStringForm);//getting -1 as response
also tried with
var index = _.indexOf(doc.players.toString().split(","), idInStringForm);//it is working
but when I needed to take union of ObjectIds' arrays the above logic fails for example:
var arrayOfIds = _.union(doc.players.toString().split(","), stringArrayOfIds);
the above is not working because when doc.players is empty the arryaryOfIds also contains " " which fails my one of queries.
Does we have any better/common solution for the above cases or we need to go with if-else check?
You can filter out any empty strings before checking the union of the array and the id.
function removeEmptyStrings(value) {
return value != "";
}
array = array.filter(removeEmptyStrings);
Wherever the array value is equal to an empty string it will remove it from the array.
See the documentation for the filter method here: Filter
Why not Array.prototype.map and then union?
_.union(doc.players.map((player) => player._id), stringArrayOfIds)
I am trying to build query string to find documents in mongodb.
I have a variable assigned for the value in the key:value pair.
My code looks like below
var query = {"_id":orgId[0]};
var name = "_id";
console.log(query);
db.collection('orgs')
.findOne({query}, function (err,result) {
});
The value of query looks like the below
{ _id: '"567a701be4b017"' }
Its basically adding a single quote around the string value and therefor the result from the query is null. How can i get past this ?
Thanks
Sam