how to query JSON in mongodb - mongodb

I have following JSON. I want to store it in MongoDB as Json and query it. How can I do it?
JSON
{
"id": 4,
"user": {
"firstname":"Finn",
"lastname":"Balor",
"email":["fb#wwe.com","fb1#wwe.com"],
"password":"whateverHisTaglineIs",
"address":{
"street":"64 victoria street",
"country":"UK"
}
}
}
I am storing this as the following document
> db.users.insert({id: 4,user: {firstname:"Finn",lastname:"Balor",email:["fb#wwe.com","fb1#wwe.com"],password:"whateverHisTaglineIs",address:{street:"64 victoria street", country:"UK"}}})
how can I query this record using country as selector?

You need to query like below, go through MongoDB documentation and learn MongoDB from MongoDB University.
db.users.find({"user.address.country": "UK"})

Related

Why can't I insert my data into my MongoDB database?

I have exported one of my collections from MongoDB and it looks like the following:
[
{
"_class": "test.Tag",
"title": "My Title",
"_id": {
"$oid": "637a2bc4a9c297b306dd0be9"
}
}
]
Now, I want to add another item to this collection.
I have created a json file exactly in the same format.
However, when I use the following command:
db.tag.insertMany([{"_class": "test.Tag", "title": "New Title","_id": {"$oid": "637a4171833047f9e046f3c9"}}])
I get the error:
MongoBulkWriteError: _id fields may not contain '$'-prefixed fields: $oid is not valid for storage.
I found a couple of similar questions on stackoverflow, e.g. this one, but they didn't help me.
How should I insert something in my database?
When I use MongoDB Compass, there is no problem. It works. But when I try it on my real database on the server, it doesn't work.
If it helps, I am using Mongo version 6.0.1.
Your help would be appreciated.
In the legacy mongo shell there is no helper for extended json format , but in mongosh you can use EJSON to deserialize the extended json and insert it as follow:
> db.test4.insertMany([EJSON.deserialize( {"_id": { "$oid": "637aa8243d58f36141e59c8d"} } )])
{
acknowledged: true,
insertedIds: { '0': new ObjectId("637aa8243d58f36141e59c8d") }
}
>
( The legacy mongo shell was deprecated in MongoDB 5.0 and removed in MongoDB 6.0 )

Azure Cosmos DB api SQL last mesure for each element

I have a Cosmos DB container "mesure" like this :
{
"cdreseau": "035000544",
"date": "2020-12-09",
"element": "PH",
"val": 7.1
}
{
"cdreseau": "035000544",
"date": "2020-14-09",
"element": "CA",
"val": 20.1
}...
I would like ton find last mesure value and date for each element in a "cdreseau"
I can get last date foreach element with that :
SELECT MAX(c.date) as date,c.element FROM c where c.cdreseau='040000422' group by c.element
But how can i get the c.val of the item in the same request ? /date,/cdreseau,/element is unique key
regards
This can't achieve by SQL in Cosmos DB. You can create a new container which save the latest values of your elements. Or you can implement a materialized view using change feed.
Ref: How do I get the latest record for each item in CosmosDB using SQL

get the particular field value from mongodb

In mongodb i saved document like this.
"Angela_Merkel": {
"birthPlace": "Hamburg,West_Germany",
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Angela-Merkel-2014.jpg?width=300",
"almaMater": "Leipzig_University",
"birthDate": "1954-07-17",
"spouse": "Joachim_Sauer"
}
There are many person's information like this way. Now if I want to get all the information of "Angela_Merkel" or only a particular like "birthdDate" of "Angela_Merkel" then what will be the query?
Like chridam says would be more practical that you refactor your documents like this:
{"name": "Angela_Merkel",
"birthPlace": "Hamburg,West_Germany",
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Angela-Merkel-2014.jpg?width=300",
"almaMater": "Leipzig_University",
"birthDate": "1954-07-17",
"spouse": "Joachim_Sauer"
}
Inside the "people" collection (Its a convention name the collections as plurals, beeing a set of documents)
Then you could do:
db.people.find({"name":"Angela Merkel"},{"_id":0," "birthdDate":1 })
To get:
{"birthDate": "1954-07-17"}
Anyway you can get with your document what you want this way:
"birthDate":
db.person.find({"Angela_Merkel.birthDate": "1954-07-17"})
or all the info:
db.person.find({"Angela_Merkel": { $exists: true}})
But doesn't have much sense do it this way.
PD: Edited to include last part.

Does the $slice function in MongoDB Meteor work?

Does the $slice function work in Meteor MongoDB?
Here is some query sample:
Posts.find({"permalink":"udrskijwddhigfwhecxn"},{"comments":{"$slice":10}});
I tried querying in the mini MongoDB and directly through publish using parameters, but it always return the complete nested data.
{
_id:Object(1231o2j3lkqj),
body:"this is body",
author:"machine",
permalink:"udrskijwddhigfwhecxn"
title:"this is title",
tags: ["dog","cat","tree"]
comments: [{
body:"comment body",
author:"lara",
email:"email#ab.com"
},
...]
date:ISODate("2013-03-16T02:50:27.881Z")
}
you can split the following. by using The projection in Meteor and it is specified by fields.
Posts.find({"permalink":"udrskijwddhigfwhecxn"},
{
fields:{"comments":{"$slice":1‌​0}}
})

How to Query MongoDB, only using the value of inner elements and it's path

I am new to MongoDB. And now i got a puzzle:
say i've got a query workable in mongo console
{
"_id": {
"$oid": "50a5e1cd703d7e9c65326bf9"
},
"people":{
"name":"arthur",
"tele": "001-837475"
"address":{
"country":"us",
"state" : "CA",
"city" : "LA"
}
}
}
I've got pretty many record like this. & I want to query for all people who come from CA.
The query below works well in mongo shell
db.test.find({"people.address.state":"CA"})
But I must do the query in Java.
PS: I don't want to use other opensource packages. just the mongodb-java-driver would be delightful.
Thanks.
There should not be any problem, you can use the query in the exact same way:
DBObject query = new BasicDBObject("people.address.state", "CA");
test.find(query);