How to write a mongodb select query using (in) with C# - entity-framework

In Sql:
SELECT * FROM inventory WHERE status in ("A", "D")
MongoDB:
db.inventory.find( { status: { $in: [ "A", "D" ] } } )
How to write above mentioned query in C# with mongoDB.i am new in mongodb.Please any one help me.
Thanks in advance.

Related

Mongodb query starts with inside $in Query

I want to write an $in query if column path starts with "/v1/user/1" or "/v1/user/2" then I want to select that particular document.
The sample document is as below:
{
"_id" : ObjectId("5f8872b486aa9e79d71d9809"),
"method" : "get",
"path" : "/v1/user/1/details/"
}
I tried to create a query but getting syntax error because of "/"
db.user.find({ "path": { $in: [ /^/v1/user/1/ , /^/v1/user/2/ ] } })
Could some one please help how to write the query.
Your query was almost correct. You just missed to escape the /. Try below query.
db.user.find({ "path": { $in: [ /^\/v1\/user\/1/ , /^\/v1\/user\/2/ ] } })

Mongodb subquery in insert

I search for make something like :
insert into B (col)
select id from anotherCollection limit 1
I test this, but I suspected it was not going to work :
db.B.insert([
{
"a": "lores ipsum",
"b": [
db.getCollection('anotherCollection').find({},{_id:1}).limit(1)
]
}
]);
is it necessary to use a projection ?
You can't do it in mongo dbbut in javascript or python like this
var a = db.getCollection('anotherCollection').find({},{_id:1}).limit(1)
db.B.insert([
{
"a": "lores ipsum",
"b": a
}
]);

how to select same column twice in mongodb

Generally we can select a column in mongodb like follows,
db.TestData.find({"_id" : ObjectId("53eba30740adad086ce3599e")},{_id:1})
In sql we are selecting a same column twice like follows,
Select column1,column1 from table1
I need a mongodb query to select like above.
How can I do that?
thanks in advance. . . .
You could either use a javascript function to manipulate it, or do a basic aggregate query using the project operator:
db.accounts.aggregate({ $project: { _id: 1, _id2: '$_id' }});
// Or using a match to filter the results first
db.accounts.aggregate(
{ $match: { _id: ObjectId("53eba30740adad086ce3599e") } },
{ $project: { _id: 1, _id2: '$_id' } }
);

Query data inside embedded array in MongoDB

I've an data in a collection as below.
{
"date":"01-01-2014",
"details": [
{"name":"abc",
"address":{
"city":"abc" , "state":"abc"
},
{"name":"xyz",
"address":{
"city":"xyz" , "state":"xyz"
}
}
]
}
I want all records having city as "abc" . I am using Java.
I tried below queries and the output is null.
Query searchQuery = new Query();
searchQuery.addCriteria(Criteria.where("details.address.$.city").is("abc"));
and
searchQuery.addCriteria(Criteria.where("details.address.city").is("abc"));
Any help greatly appreciated!!

MongoDB : How to search based on two Parameters in Mongo

I have a Collection named as Orders in my Mongo DB .
Is it possible to do a search on mongo DB based on two fileds .
For example i want to search the collection based on symbol and hi
db.Orders.find({"symbol" : "AADI"})
Please let me know how can i include the other parameter hi also in the search ??
Thanks
Just have two conditions separated by commas:
e.g.
db.users.find(
{ status: "A",
age: 50 }
)
http://docs.mongodb.org/manual/reference/sql-comparison/
Mongodb provides an implicit AND , so when you do
db.inventory.find( { price: 1.99, qty: { $lt: 20 } , sale: true } )
it is same as
db.inventory.find({ $and: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: true } ] } )
For other operators you can have a look at the reference
mixing $or with logical AND
db.inventory.find( { price:1.99, $or: [ { qty: { $lt: 20 } }, { sale: true } ] } )
This query will select all documents in the inventory collection where:
the price field value equals 1.99 and
either the qty field value is less than 20 or the sale field value is true.
Other operators
You can refer to the reference for examples to other operators.
Also, once you use:
db.users.find(
{ status: "A",
age: 50 }).pretty()
Using pretty() method at the end of the statement, you make sure that the info comes with a \n per key : value